If your using virtualenv then you should be using virtualenvwrapper, it's a great tool to make it really easy to use multiple virtualenv's and work with them painlessly.

Here's the why

Virtualenv is a nice way to separate out all your python libraries into their own individual environments, virtualenvwrapper makes it easy to put all these environments into one convenient location, rather than install all your python libraries into a site wide, global location.

By default when you pip install onto your machine, these will default to your global site packages location, somewhere like: /usr/local/lib/python2.6/dist-packages (on ubuntu)

Instead if we use virtualenv's + virtualenvwrapper we can contain multiple python modules/libraries installed to a selected personal folder. I have all my virtualenv's installed in a hidden directory in my home folder.


This is defined via your shell initialisation file (in my case .bashrc)

$ grep virtualenvs .bashrc
export WORKON_HOME="$HOME/.virtualenvs"

Within this folder all my libs are installed (once I've activated that virtual environment):

simon@simon-development:/home/simon/.virtualenvs$ ls -d */
geo/  metrics/  monitor/  simonsblog/

To actually use one of these virtual environments you only have to activate the one you wish to work in, this is where virtualenvwrapper comes in really handy:

simon@simon-development:/home/simon/.virtualenvs$ workon

This list all my virtual environments, to select mine I simply:

simon@simon-development:/home/simon/.virtualenvs$ workon simonsblog

I'm then transferred into my virtual environment (simonsblog_django) and any libraries I want to install via pip or easy_install will be installed into my virtual environment and not globally, and using virtualenvwrapper.projects (as of virtualenvwrapper 2.9 there is no need to install this package separately) I also get dumped into my working folder for that project (see setvirtualenvproject).

$ ls -d /home/simon/.virtualenvs/simonsblog/lib/python2.6/site-packages/*/


Whenever I do pip install package all those files are dumped into my /home/simon/.virtualenvs/simonsblog_django/lib/python2.6/site-packages/ directory.

The only rule is this: you need to install *virtualenv* & *virtualenvwrapper* as you would normally into the global site wide packages, then once these are installed globally for everything else you can install to one of your virtual environments.

Here's the how

$ sudo aptitude install python-pip python-dev build-essential
$ sudo pip install -U pip
$ sudo pip install virtualenv
$ sudo pip install virtualenvwrapper
$ mkdir $HOME/.virtualenvs
$ vi .bashrc
export WORKON_HOME="$HOME/.virtualenvs"
source /usr/local/bin/
$ source bashrc
$ mkvirtualenv myvirtualenvtest

This will then initiate your myvirtualenvtest environment and from then on your installs (pip/easy_installs) will relate to your activated virtualenv.

My .bashrc looks something like this:

export WORKON_HOME="$HOME/.virtualenvs"
source /usr/local/bin/

# alias' from Holger Krekel
alias v=workon
alias v.deactivate=deactivate
alias'mkvirtualenv --no-site-packages'
alias v.mk_withsitepackages='mkvirtualenv'
alias v.rm=rmvirtualenv
alias v.switch=workon
alias v.add2virtualenv=add2virtualenv
alias v.cdsitepackages=cdsitepackages
alias v.lssitepackages=lssitepackages

You'll also see in my .bashrc is a PROJECT_HOME environment variable, this is where I keep my project code, which again keeps my code in a handy convenient place, check out projects in virtualenvwrapper for more on this, but you can do nice things like use templates so you can start a new django project (this is an extension details here) and this will build up the necessary libraries for django within this new project.

There is loads more to virtualenv and virtualenvwrapper, you will find that are invaluable when dealing with multiple projects/sites which each need their own specific python modules and libraries.

Also check out this Pycon video that explains virtualenv.