Yan Han's blog

On Computer Technology

11 Aug 2017

The best way to install python on Mac OS X

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?

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)

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

Disclaimer: Opinions expressed on this blog are solely my own and do not express the views or opinions of my employer(s), past or present.

comments powered by Disqus