virtualenv

Portland Python Users Group

Author: Jason Kirtland
Date: February 12th, 2008

What is virtualenv?

virtualenv creates isolated Python environments.

PYTHONPATH?

You can already import "uninstalled" modules by manipulating PYTHONPATH:

PYTHONPATH=lib:more_libs:/usr/local/lib/otherlibs python something.py

virtualenv / virtualenv.py

http://pypi.python.org/pypi/virtualenv

One step install

$ mkdir foo
$ ls foo
$ python virtualenv.py foo
New python executable in foo/bin/python
Installing setuptools............done.

One step install

$ mkdir foo
$ ls foo
$ python virtualenv.py foo
New python executable in foo/bin/python
Installing setuptools............done.
$ ls foo
bin/  lib/
$ ls foo/bin foo/lib
foo/bin:
activate  easy_install*  easy_install-2.5*  python*

foo/lib:
python2.5/

Activating the virtualenv

$ which python
/usr/bin/python
$ source foo/bin/activate
(foo)$ which python
~/projects/talks/Lightning/virtualenv/foo/bin/python
(foo)$ echo $PYTHONPATH
(foo)$

Managing installation

When you use the python in your virtualenv's bin/ directory, all package installations will be routed into the virtualenv.

Installs with setup.py

(foo)$ tar zxf psycopg2-2.0.6.tar.gz
(foo)$ cd psycopg2-2.0.6
(foo)$ python setup.py install
running install
running build
[...]
(foo)$ python -c 'import psycopg2'
(foo)$ /usr/bin/python -c 'import psycopg2'
Traceback (most recent call last):
  File "<string>", line 1, in ?
ImportError: No module named psycopg2

Installs with easy_install

(foo)$ easy_install nose
Searching for nose
Reading http://pypi.python.org/simple/nose/
Reading http://somethingaboutorange.com/mrl/projects/nose/
Best match: nose 0.10.1
Downloading http://[...]/nose-0.10.1.tar.gz
[...]
(foo)$ which nosetests
~/projects/talks/Lightning/virtualenv/foo/bin/nosetests

Installing development versions

(foo)$ svn co somepackage
[...]
(foo)$ cd somepackage
(foo)$ python setup.py develop

Okay, now what do I do with it?

  1. Keep your source code in your virtualenv
$ cd ~/projects/myproj
$ source bin/activate
(myproj)$ ls
bin/  lib/  myproj/  setup.py  tests/
(myproj)$ <hack hack hack>

Okay, now what do I do with it?

  1. Don't keep your source code in your virtualenv
$ source ~/environments/myproj/bin/activate
(myproj)$ cd ~/projects/myproj
(myproj)$ ls
myproj/  setup.py  tests/
(myproj)$ <hack hack hack>

Okay, now what do I do with it?

  1. Use the virtualenv without activating it
$ ~/projects/foo/bin/python -V
Python 2.5.1

This is great for the #! line of scripts

Okay, now what do I do with it?

  1. Use it in mod_wsgi, mod_python or other embedded interpreters

Just put the virtualenv's site-packages directory on the path

import site
site.addsitedir('/home/jek/projects/foo/lib/python-2.5/site-packages')

Okay, now what do I do with it?

  1. Bundle it with your source code

Include a bin/virtualenv.py in your source control system, and run it on new checkouts to set up a development environment.

virtualenv also provides an API that allows you to generate a version of the virtualenv.py script customized with any additional logic you might like, such as installing project pre-requisites into the virtualenv.

virtualenv for code library development

What virtualenv 1.0 doesn't do

What else does virtualenv do?

It works on Windows too.

virtualenv / virtualenv.py

http://pypi.python.org/pypi/virtualenv

Lots of on-line resources: mailing list, bug tracker, IRC

Increasingly, I see virtualenv recommended as a best practice for developing with web frameworks. If you do web development, you may find additional support in your framework community.

Summary

  • A simple, powerful tool
  • Environments are cheap and disposable
  • Deploy services with a known set of libraries on any system
  • Easily test your code against new versions of libraries
  • Develop multiple projects simultaneously without conflicts

Thanks!

Date:February 12th, 2008
Copyright:Jason Kirtland
License:Creative Commons Attribution-Share Alike 3.0
Made With:Python, reST, Pygments and rst2s5
Version:1
ui/cc.png