Update: I’ve recently switched from Macports to Homebrew. I describe my new configuration in this post.
Here’s a rundown of how I get multiple versions of Python working on OSX. I’m running Snow Leopard on an older Macbook Pro that can only run in 32-bit mode. If you’ve got a newer 64-bit Mac, your mileage may vary. In the next month or so, work will be providing me with a new iMac. If I run into any issues using this technique on that 64-bit machine, I’ll update this post.
If you don’t already have it installed, grab the DMG file for Macports. Verify that you have Xcode installed, and optionally, the X11 User package if you plan to install ports that use X-Windows (X11 User is installed by default on Leopard and Snow Leopard).
Now that you’ve got Macports installed, its time to install the versions of Python you want to work with. I’ll be describing the steps you’ll take at the Terminal to get all versions of Python that Macports supports installed, but you could just as easily use a Macports GUI like Porticus to install these packages. You might also only want a subset of these packages installed.
From a shell prompt, enter the following commands.
sudo port -v selfupdate sudo port install python24 sudo port install python25 sudo port install python26 sudo port install python27 sudo port install python31 sudo port install python_select
At the time his post was written, executing these commands would install Python 2.4.6, 2.5.5, 2.6.6, 2.7, and 3.1.2, along with python_select into /opt/local/bin. Running python_select with the -l option from the Terminal should result in something like this.
[dale@DaleBook ~]$ python_select -l Available versions: current none python24 python25 python26 python26-apple python27 python31
On Snow Leopard, you’ll see a python26-apple option like above. On Leopard, you’ll see python25-apple. This is the system Python installation.
If you haven’t done so already, now it is time to install virtualenv and virtualenvwrapper into your system Python. Now, you don’t have to install them into your main Python. You could switch to another one of the installed versions with python_select, but then you’d have to remember to switch to that version before using your virtual environments, and you might encounter configuration issues with editors. I just find installing these into the system Python results in less headaches.
Both of these are installable via Macports, easy_install, or pip. I’ll stick to the easy_install route, although I tend to favor pip when working with the virtual environments themselves.
sudo easy_install virtualenv sudo easy_install virtualenvwrapper
Pick a location to store your virtual environments in (I use ~/Envs), then add an export statement to your ~/.profile file to point WORKON_HOME to this location. See the virtualenvwrapper docs for more information on configuring this if you run into problems.
Create and Use a Virtual Environment
Now lets try it all out. Let’s create a virtual environment running Python 2.7. From the Terminal, enter the following:
mkvirtualenv --no-site-packages --python=/opt/local/bin/python2.7 py27
This should result in a new virtual environment based on Python 2.7. By changing the path specified with the –python option, you’ll be able to create a clean install of any of the versions of Python supported by Macports. Again, see the virtualenvwrapper documentation for more information on how to switch between and manage your virtual environments.