As I wrote previously, virtualenv in Python simplifies development life and dependency handling by allowing you to effectively use a fresh Python install for each project you work on.

In turn, virtualenvwrapper simplifies the use of virtualenvs! (Thanks Morty for pointing it out!) Like my previous post, I'll keep any exposition to the minimum.

How do you install virtualenvwrapper?

On my Ubuntu 12.04 LTS system, I installed it via the apt package manager. It was a little bit more of a pain since it sticks it in a non-standard location:1 not the expected /usr/local/bin/virtualenvwrapper.sh but the unexpected /etc/bash_completion_d/virtualenvwrapper.

Once installed, add the following two lines to your shell startup file e.g. .bashrc

source /etc/bash_completion_d/virtualenvwrapper
export WORKON_HOME=~/path/to/your/virtualenvs

(Replace /etc/bash_completion_d/virtualenvwrapper in the source line with /usr/local/bin/virtualenvwrapper.sh depending on where the virtualenvwrapper script is actually located.)

Save your newly modified .bashrc and re-source it by entering in your bash terminal: source ~/.bashrc

virtualenvwrapper is now ready to go!

What does virtualenvwrapper offer?

After using it briefly, the stand out features are:

List your available virtualenvs (provided they are located in $WORKON_HOME): workon

Quick switch to a virtualenv: workon virtualenvname

Another really cool thing with virtualenvwrapper is that it allows you to specify preactivate and postactivate scripts. This is really useful. One handy shortcut is to make workon not only switch to a particular virtualenv, but to also change directory to whereever the virtualenv is.

This makes a lot of sense: it's probably more often than not that you'd want to switch to the virtualenv without working on that project.

To set this up: do cd $VIRTUALENVWRAPPER_HOOK_DIR

You'll find the postactivate file in there. Add the following two lines to it:

proj_name=$(echo $VIRTUAL_ENV|awk -F'/' '{print $NF}')
cd ~/project_dir_name/$proj_name

(taken from http://pynash.org/2013/02/18/quick-hit-virtualenvwrapper-auto-dir.html)

Replace ~/project_dir_name with the path to your Python virtualenvs.

Even in my case, where I only have a few projects right now, this shortcut saves you a lot of typing cd and activate commands.

Make a new virtualenv in $WORKON_HOME (this also activates it):

mkvirtualenv newvirtualenvname

Delete a virtualenv:

rmvirtualenv virtualenvname

Other interesting virtualenv management features I've not used yet:

Show installed packages in currently active virtualenv:

lssitepackages

Wipe third party packages from the current virtualenv:

wipeenv

Add directories to the PYTHONPATH of the current virtualenv:

add2virtualenv directory1 directory2

Toggle the current virtualenv's access to your globally installed Python packages:

toggleglobalsitepackages

Run a command in all virtualenvs in WORKON_HOME:

allvirtualenv command

There are further features detailed in the documentation, but these tricks alone make taking the time to install it worthwhile.


  1. I also ran into a strange issue whereby in this confusion I removed virtualenvwrapper, reinstalled it, but the virtualenvwrapper file in bash_completion_d didn't reappear. I had to apt-get purge virtualenvwrapper and then sudo apt-get install virtualenvwrapper to fix this. (Also note that if you have pip installed, you can do sudo pip install virtualenvwrapper, which presumably installs it to the standard location and should circumvent this problem entirely.)