This guide shows how to install the latest 2.x release of Python and all important Python packaging / sandboxing utilities like Pip and Virtualenv under Mac OS X 10.10 Yosemite.

I am using Homebrew to install pre-compiled software. I checked Homebrew 4 years ago and it had issue but now in 2015 it became a more mature solution for the installation of complex Unix software under Mac.

Let’s start.





1. Install Homebrew

Open Terminal.app and type (copy & paste) the following lines to install Homebrew and to check if it worked:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew help echo "export PATH=/usr/local/bin:/usr/local/sbin:$PATH" >> ~/.profile source ~/.profile

the brew help command should display a list of brew (= Homebrew) commands as validation of a successful installation of Homebrew.





2. Install Python 2.7.9 & Pip

Enter in Terminal.app:

brew install python python -V

The newly installed Python version should be displayed. That was easy!

Now you need update Pip which is a Python package manager. It was already installed together with Python.





3. Update Pip

Enter in Terminal.app:

sudo pip install --upgrade pip pip -V

The Pip version is displayed and can be used to install and upgrade all further Python packages and libraries.

But one key component of every Python development environment is still missing: Virtualenv. Virtualenv is something like a sandboxing system which separates applications (your Python projects) and their installed packages from each other. That separation is very important due because Python has its own internal (not so easy to understand) package dependency system.

All that becomes important latest when you want to deploy your project and its dependencies so better do sandboxing from Day 1.





4. Install Virtualenv & Virtualenvwrapper

Let’s start sandboxing by installing the necessary tool Virtualenv (“virtual environments”). Enter in Terminal.app:

sudo pip uninstall virtualenv sudo pip uninstall virtualenvwrapper sudo pip install virtualenv==1.6.4 sudo pip install virtualenvwrapper mkdir ~/.virtualenvs echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.profile echo "export WORKON_HOME=~/.virtualenvs" >> ~/.profile source ~/.profile

Correct, you just uninstalled it before installing! That is necessary because we also install Virtualenvwrapper which may require a certain Virtualenv version.

The second part of all the entered commands created a hidden file in your home directory which contains the Virtualenv settings. And as last step you created autostart magic of all that.





5. Finished, now let’s test!

Congratulations, you successfully installed Python 2.7.9 (or later), Pip and all sandboxing tools under Mac OS X 10.10 Yosemite.

Now it is time to create our first test project and sandbox (often called virtualenv but that may be confusing due to the Virtualenv project).

Enter in Terminal.app:

cd ~ mkdir PythonProjects cd PythonProjects mkdir test1 cd test1

You created a new folder called PythonProjects in your home folder and a subfolder called TestProject. You can browse that still empty folder in Finder, too.

Now it is time to create the sandbox of TestProject which we call test1. Enter in Terminal.app:

cd ~/PythonProjects/test1 mkvirtualenv test1

You just made / created a virtualenv sandbox called test1. Now it is necessary to work on that sandbox before you do any Pip operation.

Good to know: The name of the active Sandbox is displayed directly at the beginning of the Terminal command prompt which is very helpful to see if you are currently inside a sandbox or not!

To leave / close the sandbox just type deactivate . So let’s continue with the test by activating the sandbox, installing an the Python package PyTest (just as example), test all that and deactivate / close the sandbox:

workon test1 pip install pytest python -c 'import pytest;print pytest.__version__' deactivate

The package PyTest was installed because the call at line 3 displayed the version. Now we left the sandbox and if we do the same call again then the package should not be available anymore (because we just installed it inside the test1 sandbox) and an ImportError is displayed:

python -c 'import pytest;print pytest.__version__'

So always keep in mind to enter workon before you do any Pip operation or start working on a project. Also check the installed Virtualenvwrapper, it comes with some further handy commands and automation.

And finally, delete the sandbox because it was just a test:

rmvirtualenv test1

I hope you enjoyed that guide and it was helpful. Please leave a comment if it worked and also if not.

Thanks!