Easily changing virtualenvs
The workon script available in virtualenvwrapper is a useful tool for switching virtual environments on the fly. However, it fails for the workflow I prefer, so I wrote my own.
I prefer to keep my virtualenvs in the same folder as whatever source code or project they apply to. Normally, I put them in a folder named venv, although sometimes I use different names, most often when I have multiple virtualenvs applying to the same source tree (e.g. venv27, venvpypy).
My directory structure therefore looks like this:
/home/dusty/code/ + project1 | + src | | + pkg1 | | + pkg2 | + doc | + venv + project2 | + src | + venv27 | + venvpypy + project3 | + venv
... and so on
The problem is, I may be anywhere in the folder tree when I want to activate a virtualenv. This means I'm doing stuff like . ../../venv/bin/activate one time and venv/bin/activate another time. Trying to remember which parent directory I want often requires several tab completions to see the directory listing. This is annoying. So I wrote a bash function to activate the optionally named virtualenv in the current directory or any parent directory.
I avoid bash whenever I can, so this may not be the prettiest bash script you've ever seen. ;-)
function v { # activate a virtualenv # usage: v # activate the virtualenv named venv # usage: v venvname # activate the virtualenv named venvname name=venv if [ $1 ] ; then name=$1 fi olddir=$(pwd) quit=0 deactivate &>/dev/null cwd=$(pwd) while [ $quit -eq 0 ] do cd $cwd if [ $cwd == '/' ] ; then quit=1 fi if [ -e $name ] ; then source "$name/bin/activate" quit=1 fi |
Put this in your ~/.bashrc. Typing v on the command line will look in the current and all parent directories for a directory named venv, and will activate that venv. Typing v [venvname] will walk the same tree looking for a directory named [venvname] to activate.
If you want to make it prettier, feel free to fork it on github.


