insomnihack

When sleep eludes, the keyboard beckons

Multiple Python Versions on OSX with Virtualenv and Homebrew

June 1, 2011 Dale 2 Comments

Since writing my post on configuring multiple versions of Python on OSX with Virtualenv and Macports, I’ve switched from Macports to Homebrew as my package manager for Unix utilities on OSX. I did this for a couple of reasons. Homebrew seems to install less dependencies than Macports or Fink. Also, most of my Python development has settled on Python 2.7 and occasionally Python 3 for tasks that don’t require external libraries that only run on Python 2.x. So for me, an easier install with less gunk on my hard drive is a fair trade for losing the latest updates to Python 2.4, 2.5, and 2.6. In this post, I’ll describe my setup.

The first thing I did was uninstall Macports. Homebrew has fairly up-to-date versions all of the Unix packages I use on a daily basis, including git, subversion, bash_completion, Python, Qt, PyQt, and their supporting libraries. As such, I don’t need Macports taking up additional hard drive space and potentially causing conflicts with Homebrew’s packages.

Next, I installed (or update) pip, virtualenv, and virtualenvwrapper in my Mac’s default Python (2.6.1 on Snow Leopard) via easy_install (or pip itself). These are usually the only 3rd-party Python packages I install in the system Python’s site-packages folder.

The next step is to brew install the packages I mentioned above. If you’re looking for a Unix port other than those I use, check out the formula folder on Github for the current list of packages supported by Homebrew. You can also use brew search to get a list at the Terminal (fwiw, I use iTerm).

Next, I remove the Macports configuration from my bash ~/.profile and replace it with the following:

[text]
# virtualenvwrapper
export WORKON_HOME=$HOME/VirtualEnvs
if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
source /usr/local/bin/virtualenvwrapper.sh
fi

# bash completion
if [ -f `brew –prefix`/etc/bash_completion ]; then
. `brew –prefix`/etc/bash_completion
fi

# Python 2.6.1
alias mkve26=’mkvirtualenv –no-site-packages’
# Python 2.7.1
alias mkve=’mkvirtualenv –no-site-packages –python=/usr/local/Cellar/python/2.7.1/bin/python’
alias mkveqt=’mkvirtualenv –python=/usr/local/Cellar/python/2.7.1/bin/python’
alias designer=’open /usr/local/Cellar/qt/4.7.2/bin/Designer.app’
# Python 3.2
alias mkve3=’mkvirtualenv –no-site-packages –python=/usr/local/Cellar/python3/3.2/bin/python3′

# Ammend python path for Homebrew PyQt
export PYTHONPATH=/usr/local/lib/python:$PYTHONPATH
[/text]

The first couple of sections set up the Terminal for bash completion and virtualenvwrapper commands. The third section sets up my virtualenv aliases: one for the default system python, one for Python 2.7, one for Python 2.7 and PyQt, and another for Python 3.2. Python 3 support was added to virtualenv in version 1.6, so you no longer need to jump through hoops to get it working with Pythyon 2.x.

Programming mac, Python, virtualenv

Comments

  1. Matthew Schinckel says

    June 8, 2014 at 6:31 PM

    It is rather frustrating that Homebrew unlinks old versions: it breaks things like tox being able to automatically find the old versions of python to run matrix testing.

    I solved this a slightly different way: http://schinckel.net/2014/06/09/multiple-homebrew-pythons/

Trackbacks

  1. Ansible = “Infrastructure as Data, Not infrastructure as Code” says:
    April 25, 2012 at 1:42 AM

    […] I will start with installing Ansible on MacOS. First, it’s best to setup virtualenv and virtualenvwrapper. A good tutorial for this is Multiple Python Versions on OSX with Virtualenv and Homebrew […]

Leave a Reply

Your email address will not be published. Required fields are marked *

About Me

Hi, my name's Dale, and this is my tech blog. Read More…

Search

Tags

blogging c# database Django documentation Email furniture git hacks htpc IPython Linux mac nas packaging PyCharm Python Roleplaying tools twitter unit tests virtualenv Visual Studio vmware Windows

Copyright © 2025 · Equilibre Theme on Genesis Framework · WordPress · Log in