Author: | Jason Kirtland |
---|---|
Date: | February 12th, 2008 |
virtualenv creates isolated Python environments.
You can already import "uninstalled" modules by manipulating PYTHONPATH:
PYTHONPATH=lib:more_libs:/usr/local/lib/otherlibs python something.py
http://pypi.python.org/pypi/virtualenv
$ mkdir foo
$ ls foo
$ python virtualenv.py foo
New python executable in foo/bin/python
Installing setuptools............done.
$ 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/
$ which python
/usr/bin/python
$ source foo/bin/activate
(foo)$ which python
~/projects/talks/Lightning/virtualenv/foo/bin/python
(foo)$ echo $PYTHONPATH
(foo)$
When you use the python in your virtualenv's bin/ directory, all package installations will be routed into the virtualenv.
(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
(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
(foo)$ svn co somepackage
[...]
(foo)$ cd somepackage
(foo)$ python setup.py develop
$ cd ~/projects/myproj
$ source bin/activate
(myproj)$ ls
bin/ lib/ myproj/ setup.py tests/
(myproj)$ <hack hack hack>
$ source ~/environments/myproj/bin/activate
(myproj)$ cd ~/projects/myproj
(myproj)$ ls
myproj/ setup.py tests/
(myproj)$ <hack hack hack>
$ ~/projects/foo/bin/python -V
Python 2.5.1
This is great for the #! line of scripts
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')
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.
Work out of the box with Python 2.3.
You need to find the 2.3-compatible version of subprocess and stick it in the path somewhere before running virtualenv.py.
Put pydoc in the virtualenv
You can (awkwardly) (foo)$ python `which pydoc` or just put a pydoc with the correct #! line in your bin/ (it's a 3 line script)
It works on Windows too.
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.
- 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
Date: | February 12th, 2008 |
---|---|
Copyright: | Jason Kirtland |
License: | Creative Commons Attribution-Share Alike 3.0 |
Made With: | Python, reST, Pygments and rst2s5 |
Version: | 1 |