The best way to install python on Mac OS X

11 Aug 2017, by Pang Yan Han

NOTE: This post is pretty much opinion based on my own experience. Also take note of the date it was written because things might have changed since then. Your mileage may vary.

The best way to install python on Mac OS X is using pyenv. Why do I say so?

  • Mac OS X ships with python but does not seem to ship with virtualenv
  • You can install python using brew but again it does not seem to ship with virtualenv

To me, using a sandbox environment like virtualenv or conda is absolutely essential for pretty much any project that makes use of 3rd party libraries because I do not want to install any 3rd party library globally if possible. I am more familiar with virtualenv so most of the time it is my choice.

Installation instructions

  1. Follow the instructions at https://github.com/pyenv/pyenv#homebrew-on-mac-os-x to install pyenv using brew
  2. Follow the instructions at https://github.com/pyenv/pyenv-virtualenv#installing-with-homebrew-for-os-x-users to install pyenv-virtualenv using brew
  3. Now we need to install some version of python using pyenv. To see a list of python versions you can download and install using pyenv, run:
pyenv install -l

Suppose you want to install Python 3.5.1 . Run:

pyenv install 3.5.1
  1. Suppose you have a project called myproject you want to create a virtualenv for. This project happens to use Python 3.5.1 . To create this virtualenv, we run:
pyenv virtualenv 3.5.1 myproject

Now if you run pyenv versions, you should see something like the following:

* system (set by /home/tom/.pyenv/version)
2.7.11
3.5.1
3.5.1/envs/myproject
myproject

So we see that the virtualenv myproject is created successfully.

  1. To install libraries into the myproject virtualenv, we have to activate it using
pyenv activate myproject

You should see a (myproject) prefix in your shell’s prompt.

  1. As long as you have the virtualenv activated, any Python libraries you install using pip will be installed into the virtualenv. Just run pip as per normal, for instance:
pip install redis==2.10.5
pip install -r requirements.txt
  1. To deactivate the virtualenv:
pyenv deactivate
comments powered by Disqus