Endless Obsession

better code

Getting started with virtualenvwrapper

A while back I toyed with using virtualenv and virtualenvwrapper to manage isolated python packages for development, but I didn’t get very far at the time. Recently I’ve been dabbling in ruby and have used RVM to manage ruby environments and gems. This experience has been a positive one, so I decided to revisit virtualenvwrapper to see how it would compare.

Installing

Note that I have tested this process on Ubuntu 10.04 and 11.04. I have not tested on other environments. Windows setup may follow in a later post.

The basic steps that I used are…

  • Install python
  • Install virtualenv
  • Install virtualenvwrapper
  • Configure virtualenvwrapper
  • Create a new virtual environment
  • Install packages

A script to accomplish most of this can be found here @/linux/virtualenvwrapper.sh.

1) Make sure that the script can be executed:

$ sudo chmod +x virtualenvwrapper.sh

2) Execute the script:

$ ./virtualenvwrapper.sh

3) Initialize virtualenvwrapper:

The script mentioned above adds the following to ~/.bashrc, but will not take effect until you open a new terminal window. I’m sure there’s a way to avoid having to do this. In the code below, ~/.virtualenvs can be changed to any path you wish. Also, the path may vary (link).

export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

4) Create a new environment:

mkvirtualenv env

5) Begin working on your new environment:

workon env

6) Install packages

This can be done using pip (or easy_install, I believe) and the packages are installed in $WORKON_HOME/env/…

You can list all of your required packages in a single pip requirements file and install at once.

Example:

Django==1.1.2
django-tagging==0.3.1
South==0.7.1

Then, use pip to install the packages:

pip install -r /path/to/requirements

Update 12/29/2010: Using a specific version of python would be nice, especially since it’s something that can be done easy enough with RVM. I will be investigating this so look out for a future post on the topic.

Comments