Virtualenv Wrapper Helper

March 28th, 2009

A bit inspired by all the awesome stuff happening at PyCon, I’ve done a bit of hacking on my shell to improve the use of virtualenv_wrapper. Remembering to checkout the correct virtualenv is a bit of a pain, especially when you move around your filesystem as much as I do. To fix this, I’ve written a bit of code for my bash prompt to ease the pain.

It works by looking for the existance of a .venv file every time you cd into a directory. If it exists, it will attempt to workon the contents of the file, which should be the project name. For instance, for my oebfare project (more news forthcoming on that), the file contains the words “oebfare” as that’s the name of my virtualenv. Due to this, you can get general awesomeness such as the following:

# prompt edited to save space
12:02 AM: (~/Code/django) jlilly$ cd baconfile/
(baconfile):12:03 AM: (~/Code/django/baconfile) (master) jlilly$ cd ..
(baconfile):12:03 AM: (~/Code/django) jlilly$ cd oebfare/
(oebfare):12:03 AM: (~/Code/django/oebfare) (master) jlilly$

When we cd into the baconfile dir, we get the virtualenv for baconfile. We can cd out of that directory and about the filesystem, but when we enter a directory that contains a different .venv file, we get a different virtualenv.


Comments are disabled. If that bothers you, please contact me on twitter at @justinlilly and let me know.