Saturday, April 11, 2009

Virtualenv

If you cannot be the root, or system wide Python installation have a package which version is incompatible with the one you need, you can use Virtualenv.
By executing virtualenv.py with an arbitrary directory name, you can make a local Python environment, in which there is its own python executable and easy_install in the bin directory. When you install a package with the easy_install, it installs it to the lib/python2.5/site-packages. Of course you can still use the default python installation without being affected by whatever you have installed in your local virtual python.


$ python virtualenv.py vpsandbox
$ cd vpsandbox/
$ bin/easy_install pylons
$ bin/easy_install QuickWiki==0.1.6
$ bin/paster make-config QuickWiki test.ini
$ bin/paster setup-app test.ini
$ bin/paster serve test.ini

I haven't tried but I found distutils.cfg in lib/python2.5/distutils, it should be possible to make easy_install use any package repository as well as PYPI(Python Package Index aka cheeseshop) by adding its URL in the file.

$ cat ./lib/python2.5/distutils/distutils.cfg
# This is a config file local to this virtualenv installation
# You may include options that will be used by all distutils commands,
# and by easy_install. For instance:
#
# [easy_install]
# find_links = http://mylocalsite

More on
http://wiki.pylonshq.com/display/pylonscookbook/Using+a+Virtualenv+Sandbox
http://pypi.python.org/pypi/virtualenv
http://blog.ianbicking.org/2007/10/10/workingenv-is-dead-long-live-virtualenv/

Here is the sys.path in my virtualenv Python. I've installed pylons here so it's not a brand new virtual Python environment.

>>> for p in sys.path:
... print p
...

/home/kotamura/vpsandbox/lib/python2.5/site-packages/setuptools-0.6c9-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/Pylons-0.9.7-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/Tempita-0.2-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/WebTest-1.1-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/WebError-0.10.1-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/WebOb-0.9.6.1-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/Mako-0.2.4-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/nose-0.10.4-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/decorator-3.0.0-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/simplejson-2.0.8-py2.5-linux-x86_64.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/FormEncode-1.2.1-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/PasteScript-1.7.3-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/PasteDeploy-1.3.3-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/Paste-1.7.2-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/Beaker-1.3-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/WebHelpers-0.6.4-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/Routes-1.10.3-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/Pygments-1.0-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/QuickWiki-0.1.6-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/docutils-0.4-py2.5.egg
/home/kotamura/vpsandbox/lib/python2.5/site-packages/SQLAlchemy-0.5.3-py2.5.egg
/home/kotamura/vpsandbox/lib64/python25.zip
/home/kotamura/vpsandbox/lib64/python2.5
/home/kotamura/vpsandbox/lib64/python2.5/plat-linux2
/home/kotamura/vpsandbox/lib64/python2.5/lib-tk
/home/kotamura/vpsandbox/lib64/python2.5/lib-dynload
/usr/lib/python2.5
/usr/lib64/python2.5
/home/kotamura/vpsandbox/lib/python2.5/site-packages
/usr/lib/python2.5/site-packages


Info from an anonymous:
You can run "source bin/activate" to change PATH env. etc.

2 comments:

Anonymous said...

you need to run "source bin/activate", silly.

hohehohe2 [at] gmail.com said...

Cheers, I've made an equivalent by myself.