Distutils (Python Distribution Utilities) has special support for
distrubuting and installing packages.
In this section we’ll learn how to use Distutils to package and
install a distribution that contains a single package with multiple
modules.
As our example, imagine that we have a directory containing the
following:
- Testpackages
- Testpackages/README
- Testpackages/MANIFEST.in
- Testpackages/setup.py
- Testpackages/testpackages/__init__.py
- Testpackages/testpackages/mod1.py
- Testpackages/testpackages/mod2.py
Notice the sub-directory Testpackages/testpackages containing the file
__init__.py. This is the Python package that we will install.
We’ll describe how to configure the above files so that they can be
packaged as a single distribution file and so that the Python package
they contain can be installed as a package by**Distutils**.
The MANIFEST.in file lists the files that we want included in our
distribution. Here is the contents of our MANIFEST.in file:
include README MANIFEST MANIFEST.in
include setup.py
include testpackages/*.py
Download as text (original file name:
Examples/Testpackages/MANIFEST.in).
The setup.py describes to Distutils (1) how to package the
distribution file and (2) how to install the distribution. Here is the
contents of our sample setup.py:
#!/usr/bin/env python
from distutils.core import setup # [1]
long_description = 'Tests for installing and distributing Python packages'
setup(name = 'testpackages', # [2]
version = '1.0a',
description = 'Tests for Python packages',
maintainer = 'Dave Kuhlman',
maintainer_email = 'dkuhlman@rexx.com',
url = 'http://www.rexx.com/ dkuhlman',
long_description = long_description,
packages = ['testpackages'] # [3]
)
Download as text (original file name: Examples/Testpackages/setup.py).
Explanation:
- We import the necessary component from Distutils.
- We describe the package and its developer/maintainer.
- We specify the directory that is to be installed as a package. When
the user installs our distribution, this directory and all the modules
in it will be installed as a package.
Now, to create a distribution file, we run the following:
python setup.py sdist --formats=gztar
which will create a file testpackages-1.0a.tar.gz under the directory
dist.
Then, the user, who wishes to install this file, can do so by
executing the following:
tar xvzf testpackages-1.0a.tar.gz
cd testpackages-1.0a
python setup.py build
python setup.py install # as root