Last Updated: February 25, 2016
· isaka

Setup Python Virtualenv on OS X Mountain Lion

As the name implies, virtualenv is useful if it allows you to run a complete setup of Python without impacting the rest of your system. I wanted to be sure of that, so I didn’t want anything installed in system wide library locations. Here is how I setup virtualenv on my MBP.

Configure a local Python library

$ mkdir ~/lib
$ mkdir ~/lib/python2.7
$ mkdir ~/lib/python2.7/site-packages
$ export PYTHONPATH=”~/lib/python2.7/site-packages”

To be sure the variable setting is persistent, I add it to my .bash_profile. Pretty standard stuff, I use vi to do this.

Download the PyPI setup package

Since I have python 2.7 installed, download the package setuptools-0.6c11-py2.7.egg (notice the string ‘2.7’ in the file name) from http://pypi.python.org/pypi/setuptools/.

Install PyPI

Change to your downloads location, install PyPi specifying the local Python library in the install prefix.

$ sh setuptools-0.6c11-py2.7.egg --prefix=~

Once this is done, virtualenv can now be installed. I picked up a development version here.

Install a version of virtualenv

$ easy_install --prefix=~ https://github.com/pypa/virtualenv/tarball/develop

After completing these four steps, you have a properly configured virtualenv where you can deploy Python libraries and applications without affecting your default system behaviour.

If you’re unfamiliar with Virtualenv, or that you’d like find out more about it, check the web site official http://www.virtualenv.org/en/latest/index.html