.. include::
===========================================
An Introduction to the Virtualenv Sandbox
===========================================
:Author: Jeff Rush
:Copyright: 2008 Tau Productions Inc.
:License: Creative Commons Attribution-ShareAlike 3.0
:Date: March 13, 2008
:Series: Python Eggs and Buildout Deployment - PyCon 2008 Chicago
A short intro to sandboxing your Python development work using the
*virtualenv* tool and as a prerequisite, the steps for setting up EasyInstall
to download it.
.. container:: handout
.. image:: /home/caster/themes/talk-pycon/pycon-chicago.png
:align: center
.. footer:: Python Eggs and Buildout Deployment - PyCon 2008 Chicago
Roadmap of Topics
=================
.. container:: slide-display
.. class:: incremental
+ Installation Concerns
+ About Virtualenv
+ Steps: Installing EasyInstall then VirtualEnv
+ Creating and Using Sandboxes
+ Directory Layout of a Sandbox
+ Limitations of Virtualenv
+ For More Information
.. contents:: Index of Slides
:class: handout
Installation Concerns
=====================
.. container:: slide-display
.. class:: incremental
+ Prerequisites
.. class:: incremental
+ at least Python 2.3.5
+ access to the Internet
+ system administrator access rights
+ Items of Note
.. class:: incremental
+ installs into system site-packages
+ ... of the instance of Python used
+ named with suffix of Python version
.. container:: handout
In this class we'll use *virtualenv* to create sandboxes for our exercises,
to avoid disrupting the system installation of Python.
To simplify the installation of *virtualenv*, we'll first install
*EasyInstall*, bundled with *setuptools*, which I'll cover much more
in-depth in the section on eggs.
You need a relatively modern version of Python, and access to the Internet
for retrieving the necessary files. Because we'll be installing this
system-wide (i.e. the sandboxing tools cannot themselves be inside the
sandbox), you also need administrator privileges on your system.
These two packages, *EasyInstall* and *Virtualenv*, are being installed
into the system site-packages directory, of the instance of Python with
which you invoke it. This is true of a lot of tools we'll use today.
For those with multiple versions of Python on their system, to distinguish
beween them, the tools are installed with a suffix of the version of Python
used.
About Virtualenv
================
.. container:: slide-display
.. class:: incremental
+ written by Ian Bicking
+ a fresh, isolated Python environment
+ except it pre-installs *setuptools*
+ must run programs from **bin/** (Scripts/)
+ may be
.. class:: incremental
+ in front of system Python
+ or totally replace system Python
.. container:: handout
Written by Ian Bicking, *virtualenv* allows you to set up an isolated
Python environment whose libraries do not affect programs outside it,
making it a good choice for experimenting with new packages or to deploy
different programs with conflicting library requirements.
To maintain the isolation, Python programs must be run from the "bin/"
subdirectory ("Scripts/" under Windows).
Normally your *virtualenv* sandbox is isolated from your system Python, but
requests for modules not found within the sandbox flow through to the
system Python. This means if you install extra software into the system
Python it will automatically become available in all *virtualenv*
sandboxes.
There is an option, **--no-site-packages** that changes this behavior to
exclude the system Python from all failing searches, for stronger isolation
at the expense of having to explicitly install more dependencies.
Steps: Installing EasyInstall then Virtualenv
======================================================
.. container:: slide-display
.. class:: incremental
+ Steps to take
| `$ cd /tmp`
| `$ wget http://peak.telecommunity.com/dist/ez_setup.py`
| `$ sudo python ez_setup.py`
| `$ sudo easy_install virtualenv`
.. container:: handout
Using your favorite tool, download **ez_setup.py** into a temporary
directory and run it. This will download and install the appropriate
setuptools egg for your Python version, and create a new system command
*easy_install*.
Note: Windows users; do NOT put **ez_setup.py** inside your Python
installation. Use a temporary directory elsewhere.
The "sudo" command is the Unix-way of running the "easy_install" command
with administrator privileges.
Creating and Using Sandboxes
============================
.. container:: slide-display
.. class:: incremental
+
| `$ virtualenv pycon2008`
| `$ virtualenv --no-site-packages pycon2008`
| `$ cd pycon2008`
| `$ bin/python`
| `$ source bin/activate`
| `$ deactivate`
.. container:: handout
To create a sandbox, run the *virtualenv* command and pass to it the pathname
of a new directory in which you want it to reside.
If you want stronger isolation from the system Python, use the
**--no-site-packages** option to omit the system packages from the search
path for your sandbox.
To run within the sandbox, run the Python interpreter in the bin
directory. If you prefer to avoid typing the "bin/" prefix, *virtualenv*
provides the "activate" command to rewrite the search paths for your shell
session. The command "deactivate" reverses these changes.
Note: the "activate" command for virtualenv has nothing to do with the
concept of activating or de-activating a Python egg, which means placing it
onto the sys.path using a .pth file.
Note: On Windows you shouldn't create a *virtualenv* sandbox in a path with
a space in any name.
Directory Layout of a Sandbox
=============================
.. container:: slide-display
.. class:: incremental
::
bin/
activate
easy_install
easy_install-2.5
python
lib/
python2.5/
distutils/
distutils.cfg
.. container:: handout
Note: On Windows, the directory names are slightly different, with
"Scripts/" being used for "bin/", and "Lib/" being used for "lib/".
Warning: Virtualenv is currently incompatible with a system-wide
**distutils.cfg** and per-user **~/.pydistutils.cfg**. If you have either
of these files, *virtualenv* will put the **easy_install** command into the
**bin/** directory specified in that config file, rather than into the
sandbox where it belongs.
Limitations of Virtualenv
=========================
.. container:: slide-display
.. class:: incremental
+ easy to experiment, hard to repeat and manage
+ no simple way to list what you've installed
+ no simple way of uninstalling packages
+ the *buildout* tool is opposite to this
+ and has the ability to manage non-Python aspects
For More Information
====================
.. container:: slide-display
.. class:: incremental
+ Place to Download EasyInstall bootstrapper
| wget http://peak.telecommunity.com/dist/ez_setup.py
..
+ Home Page for VirtualEnv
| http://pypi.python.org/pypi/virtualenv
..
+ the Distutils-SIG and Mailing List
| http://www.python.org/sigs/distutils-sig/
| http://mail.python.org/pipermail/distutils-sig/
..
+ Questions?
..
XXLocal Variables:
mode: rst
mode: outline-minor
End: