System Python, Python, and Virtualenv

If you use both a system python (without virtualenv) and a custom python (with virtualenv), it can be a bit of a pain to get virtualenv to use the right python – and what’s worse, it can create subtle errors that are hard to track down. Thankfully, the solution is very easy, if you’re using virtualenvwrapper – just a couple extra (documented but uncommon) settings, to tell virtualenv where to find the right python, virtualenv, and virtualenv-clone:

# This is common
export WORKON_HOME="$HOME/.virtualenvs"
# This is uncommon, for non-default python installations
export PYTHON27="$HOME/local/python-2.7.4/bin"
export VIRTUALENVWRAPPER_PYTHON="${PYTHON27}/python"
export VIRTUALENVWRAPPER_SCRIPT="${PYTHON27}/virtualenvwrapper.sh"
export VIRTUALENVWRAPPER_VIRTUALENV="${PYTHON27}/virtualenv"
export VIRTUALENVWRAPPER_VIRTUALENV_CLONE="${PYTHON27}/virtualenv-clone"
# And back to common (_lazy only loads the environment when you workon the first time)
source "${PYTHON27}/virtualenvwrapper_lazy.sh"

With that in place, you can use your system python with impunity, then switch to a custom-python virtualenv with the usual workon someproject call, rather than messing around with the PYTHONPATH and so forth.