A somewhat newer (and better) way of distributing Python software is
to use easy_install, a system developed by Phillip Eby as part of the
setuptools package. Many of the capabilities of
easy_install/setuptools are probably unnecessary for scientific Python
developers (although it’s an excellent way to install Python packages
from other sources), so I will focus on three capabilities that I
think are most useful for “in-house” development: versioning, user
installs, and binary eggs.
First, install easy_install/setuptools. You can do this by downloading
http://peak.telecommunity.com/dist/ez_setup.py
and running python ez_setup.py. (If you can’t do this as the
superuser, see the note below about user installs.) Once you’ve
installed setuptools, you should be able to run the script
easy_install.
The first thing this lets you do is easily install any software that
is distutils-compatible. You can do this from a number of sources:
from an unpackaged directory (as with python setup.py install);
from a tar or zip file; from the project’s URL or Web page; from an
egg (see below); or from PyPI, the Python Package Index (see
http://cheeseshop.python.org/pypi/).
Let’s try installing nose, a unit test discovery package we’ll be
looking at in the testing section (below). Type:
easy_install --install-dir=~/.packages nose
This will go to the Python Package Index, find the URL for nose,
download it, and install it in your ~/.packages directory. We’re
specifying an install-dir so that you can install it for your use
only; if you were the superuser, you could install it for everyone by
omitting ‘–install-dir’.
(Note that you need to add ~/.packages to your PATH and your
PYTHONPATH, something I’ve already done for you.)
So, now, you can go do ‘import nose’ and it will work. Neat, eh?
Moreover, the nose-related scripts (nosetests, in this case) have
been installed for your use as well.
You can also install specific versions of software; right now, the
latest version of nose is 0.9.3, but if you wanted 0.9.2, you could
specify easy_install nose==0.9.2 and it would do its best to find
it.
This leads to the next setuptools feature of note,
pkg_resource.require. pkg_resources.require lets you specify
that certain packages must be installed. Let’s try it out by
requiring that CherryPy 3.0 or later is installed:
>> import pkg_resources
>> pkg_resources.require('CherryPy >= 3.0')
Traceback (most recent call last):
...
DistributionNotFound: CherryPy >= 3.0
OK, so that failed... but now let’s install CherryPy:
% easy_install --install-dir=~/.packages CherryPy
Now the require will work:
>> pkg_resources.require('CherryPy >= 3.0')
>> import CherryPy
This version requirement capability is quite powerful, because it lets
you specify exactly the versions of the software you need for your own
code to work. And, if you need multiple versions of something
installed, setuptools lets you do that, too – see the
--multi-version flag for more information. While you still can’t
use different versions of the same package in the same program, at
least you can have multiple versions of the same package installed!
Throughout this, we’ve been using another great feature of setuptools:
user installs. By specifying the --install-dir, you can install
most Python packages for yourself, which lets you take advantage of
easy_install’s capabilities without being the superuser on your
development machine.
This brings us to the last feature of setuptools that I want to
mention: eggs, and in particular binary eggs. We’ll explore binary
eggs later; for now let me just say that easy_install makes it
possible for you to package up multiple binary versions of your
software (with extension modules) so that people don’t have to
compile it themselves. This is an invaluable and somewhat
underutilized feature of easy_install, but it can make life much
easier for your users.