New release: virtualenvwrapper
Last week I finally cleaned up the wrapper code I’ve been using with Ian Bicking’s virtualenv. The results are a set of bash functions imaginatively dubbed “virtualenvwrapper”.
The basic idea is that you source the script in your
~/.bashrc, and then you can use the functions it defines to manage your virtual environments, either from other scripts or from the command line.
mkvirtualenv is a thin wrapper around virtualenv itself, and creates environments in a special directory, which you can control through the WORKON_HOME variable. Now that I think of it, that variable should probably have a different name. Oh, well.
Most of the time, you’ll use the function
workon. Without arguments, it will list the environments you have available. With a single argument, it switches to that environment.
Once you’re done with an environment, use
rmvirtualenv to remove it.
So it’s nothing earth-shattering, but I find it very useful. I put it together because I create and delete virtual environments so frequently – one per article and column for the magazine, plus one for each of my own projects – that I really needed to enforce some sanity around them.
In addition to the organization, the extra feature
workon adds is activation hooks for the environment. Each time you switch environments,
workon looks for (and runs)
$VIRTUAL_ENV/bin/predeactivate in the current environment (before switching) and
$VIRTUAL_ENV/bin/postactivate in the new environment (after switching). The hooks are intended for saving editor state, loading new project files, etc.
For now there’s just one file, but I’m still distributing it in a versioned tarball because that’s just easier with my existing workflow and release tools. I’m not sure how to use distutils with a bash script, or even if that’s a good idea, so it’s a manual download, too.