1.7. Installation of Python Packages with Setuptools

Some of the modules we will use in this class are part of Python’s standard library. Others must be downloaded and installed before we can use them.

This is the case with dnspython. You might be able to find a Windows installer for many Python packages. In some cases, this may be the best or only option. In other cases, we can use Python to download and install the package for us. This is usually the best approach because this approach is easy, it makes certain we have the right package for our Python installation, and the packages can be easily upgraded.

The Python package we will use to install other Python packages is called setuptools. Some Python packages are only distributed with Python egg files. An egg file is a platform independent means to distribute a Python package. It is much easier for the developers to make one egg package file than to build a Windows installer, a Dedian ‘.deb’ file, a Red Hat ‘.rpm’ file, a Solaris package, etc ...

Here, we will demonstrate how to install setuptools and use it to install other Python packages.

1.7.1. Prerequisites

In addition to having Python installed, it is also necessary to update the %PATH% environment variable before proceeding to include Python’s bin and Scripts directories. (For Linux, only Python’s bin directory need be in the PATH variable.) If you installed Python in the default location (C:\Python26), then C:\Python26 and C:\Python26\Scripts needs to be added to the path. Environment variables are set in Window’s System Properties dialog box. Open Control Panel ‣ System (or right-click on My Computer and choose “Properties”). In the box that opens, click the “Advanced” tab to obtain the dialog box. Next, click the button labeled “Environment Variables”. It lists two kinds of variables – those that apply only to the current user and those that apply to the whole system. In the lower list of variables, select Path and then click on edit to add to the Path. Note that the items in the path are separated by semicolons (‘;’).

While you are editing environment variables, you might want to also edit %PATHEXT% adding .py to it’s list. This will allow you to enter just the name of a Python script (even leaving off the ‘.py’ extension) in the DOS Command prompt shell. Assuming .py files are associated with Python, which is usually accomplished when Python is installed, Python will automatically be used to run the script.

1.7.2. Install Setuptools

We want to install a package called setuptools, which includes a program called easy_install that we will use when we install other Python packages. The easiest way to install setuptools is to let Python do it for us with a script called ez_setup.py Use this link to download and save this script. Open a DOS command prompt window (Start ‣ Run, enter cmd and press enter). Use the DOS cd command to change the working directory to where this script was saved. Then run python ez_setup.py to download and install setuptools:

C:\Documents and Settings\tim\My Documents\download\> python ez_setup.py

1.7.3. Installing other Python Packages

Setuptools can actually do a lot of different things associated with the distribution of Python packages. Most of what setuptools can do is beyond the scope of what we need. One very handy tool provided with setuptools is easy_install. This tool works with a database from python.org now called PyPI for the Python Package Index. (It is sometimes called The Cheese Shop, which is a reference to a very funny Monty Python skit.) Any package which is registered on PyPI can be found, downloaded and installed with a simple invocation of easy_install. Here we show installing dnspython, which is needed for our short topic on using the DNS system for network programming. Other Python packages will be installed in a similar way.

C:\> easy_install dnspython

1.7.4. Updating a Python Package

From time to time, it may be necessary to update a previously installed package to get fixes to problems or new features. This is accomplished with the -U option to easy_install:

C:\> easy_install -U dnspython