.. include:: .. Copyright 2008, Jason Kirtland License: Creative Commons Attribution-Share Alike 3.0 ========== virtualenv ========== Portland Python Users Group --------------------------- :Author: Jason Kirtland :Date: February 12th, 2008 .. footer:: Portland Python Users Group - February 12th, 2008 What is virtualenv? =================== ``virtualenv`` creates isolated Python environments. .. class:: incremental - A directory on the filesystem - Contains Python libraries and scripts - Optionally insulated from the system's installed packages - Without requiring you to compile your own Python PYTHONPATH? =========== You can already import "uninstalled" modules by manipulating ``PYTHONPATH``: .. code-block:: sh PYTHONPATH=lib:more_libs:/usr/local/lib/otherlibs python something.py .. class:: incremental - Works, but you always get the system packages too - Installing complex packages into a custom location can be a pain - Have to always remember to set ``PYTHONPATH`` - Awkward with more than one custom directory on the path virtualenv / virtualenv.py ========================== http://pypi.python.org/pypi/virtualenv - It can be downloaded and installed into the system Python - Or download the archive and just keep ``virtualenv.py`` One step install ================ .. code-block:: sh $ mkdir foo $ ls foo $ python virtualenv.py foo New python executable in foo/bin/python Installing setuptools............done. One step install ================ .. code-block:: sh $ 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 ========================= .. code-block:: sh $ which python /usr/bin/python $ source foo/bin/activate (foo)$ which python ~/projects/talks/Lightning/virtualenv/foo/bin/python (foo)$ echo $PYTHONPATH .. code-block:: text (foo)$ Managing installation ===================== When you use the ``python`` in your ``virtualenv``'s ``bin/`` directory, all package installations will be routed into the ``virtualenv``. - Including packages with C extensions - And packages with scripts - Or using ``easy_install`` - With no further ado Installs with setup.py ====================== .. code-block:: sh (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 "", line 1, in ? ImportError: No module named psycopg2 Installs with easy_install ========================== .. code-block:: sh (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 =============================== .. code-block:: sh (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`` .. code-block:: sh $ cd ~/projects/myproj $ source bin/activate (myproj)$ ls bin/ lib/ myproj/ setup.py tests/ (myproj)$ Okay, now what do I do with it? =============================== 2. Don't keep your source code in your ``virtualenv`` .. code-block:: sh $ source ~/environments/myproj/bin/activate (myproj)$ cd ~/projects/myproj (myproj)$ ls myproj/ setup.py tests/ (myproj)$ Okay, now what do I do with it? =============================== 3. Use the ``virtualenv`` without activating it .. code-block:: sh $ ~/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? =============================== 4. Use it in ``mod_wsgi``, ``mod_python`` or other embedded interpreters Just put the ``virtualenv``'s ``site-packages`` directory on the path .. code-block:: python import site site.addsitedir('/home/jek/projects/foo/lib/python-2.5/site-packages') Okay, now what do I do with it? =============================== 5. 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 ======================================= - A virtualenv is an isolated version of a specific Python interpreter. - If you need to develop and test against multiple Python versions *and* want isolation, each interpreter version needs it's own virtualenv. - But it's easy to manage. I use a very convenient system of shell aliases, bopping back and forth between 2.3, 2.5, jython, 2.6 trunk, etc. What virtualenv 1.0 doesn't do ============================== - 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) 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! ======= .. class:: small :Date: February 12th, 2008 :Copyright: Jason Kirtland :License: Creative Commons Attribution-Share Alike 3.0 :Made With: Python, reST, Pygments and rst2s5 :Version: 1 .. image:: ui/cc.png