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.
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.
- Follow the instructions at https://github.com/pyenv/pyenv#homebrew-on-mac-os-x to install pyenv using brew
- Follow the instructions at https://github.com/pyenv/pyenv-virtualenv#installing-with-homebrew-for-os-x-users to install pyenv-virtualenv using brew
- 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
- Suppose you have a project called
myprojectyou 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.
- To install libraries into the
myprojectvirtualenv, we have to activate it using
pyenv activate myproject
You should see a
(myproject) prefix in your shell’s prompt.
- 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
- To deactivate the virtualenv: