Muffinresearch Labs by Stuart Colville

Simple Switching of VirtualEnv Environments | Comments (2)

Posted in Linux/Unix on 12th May 2009, 11:59 pm by Stuart

If you’re using Python for any kind of serious development virtualenv is highly recommended as a way to sandbox dependencies and allow you to run different libraries and or python versions for different projects. For more on Virtualenv and how to use it see VirutalEnv on PyPi

I wanted a quick way to switch virtual environments from anywhere. The result is a simple bash function that lists your available environments (albeit the assumption is you install your virtual environments in one place.) so you can choose one and activate it from anywhere.

The function should be placed in your .bashrc. The reason that this is a function and not a shell-script is that functions are executed in the current shell whereas placing the same code in a shell-script won’t work as a shell script invokes a separate process. As the environment activation alters the environment it needs to be run in the current shell to work.

function venvswitch {
VENV_DIR=~/pythonenv
cd $VENV_DIR
echo 'Choose Virtual Env:'

select dir in *;
do
    if [ -n "$dir" ] && [ -f $dir/bin/activate ]
    then
      source $VENV_DIR/$dir/bin/activate
      break
    else
      echo "Error: You choice '$REPLY' does not correspond to a virtual env."
    fi
done
cd - > /dev/null
}

Basicaly what the script does is tell you what envs you have and you just select a number to activate that environment. Sourcing the bin/activate in the chosen environment alters your path and also adds a deactivation function.

To run it (after you’ve sourced your .bashrc – source ~/.bashrc) simpy type:

venvswitch

De-activating the virtalenv is as simple as running the following (this feature is provided by virtualenv itself):

deactivate

You will notice the prompt is returned to normal and your $PATH is reverted back to what it was prior to activation.

Alternatives

Here’s some alternative VirutalEnv wrappers to try out:

Post Tools

Comments: Add yours

1. On May 14th, 2009 at 3:53 pm Don Spaulding said:

You might want to check out Doug Hellman’s excellent virtualenvwrapper functions (here: http://pypi.python.org/pypi/virtualenvwrapper ). It’s like what you’ve got here on steroids ;-)

2. On May 14th, 2009 at 4:07 pm Stuart Colville said:

@Don – thanks for the link.

I’ve seen a lot of wrappers that do a multitude of useful things but my requirements are incredibly simple and this fits my needs exactly. I appreciate that everyone elses mileage may vary, and it’s always good to consider all the options.

In the spirit of that, here’s another alternative — Justin Lilly’s automatic VirtualEnv Wrapper http://justinlilly.com/blog/2009/mar/28/virtualenv-wrapper-helper/







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Using Loggerhead with mod_wsgi|(0)

Here’s a post I wrote over on the Project Fondue Blog about our use of Loggerhead with mod_wsgi under Apache. Loggerhead is the rather nice branch viewer for bazaar branches as used on Launchpad.net.

If you’re not already subscribed to the Project Fondue blog feed then I can recommend it, as there should be some interesting posts coming out of there in the coming months (yes I’m unashamedly biased!).

Ubuntu: Turn off changing workspace with mouse wheel|(1)

I found the changing with the workspace with the mouse wheel really annoying. To disable it go to System => Preferences => CompizConfig (available if the compizconfig-settings-manager package is installed) and uncheck “Viewport Switcher” which is under the “Desktop” heading.

Photos on Flickr

© Copyright 2004-10 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.564s.