The chapters thus far have covered how to write, test and, debug the Python code. With all of that, there is only one thing that remains, that is packaging and distributing your Python libraries /and applications. To create installable packages we will use the setuptools
package which is bundled with Python these days. If you have created packages before, you might remember distribute
and distutils2
, but it is very important to remember that these have all been replaced by setuptools
and distutils
and you shouldn't use them anymore!
What types of program can we package with setuptools
? We will show you several cases:
setuptools
commandsBefore we actually get started, it is important to know how to install a package properly. There are at least four different options for installing a package. The first and most obvious is by using the plain pip
command:
pip install package
This can also be achieved by using setup.py
directly:
cd package python setup.py install
This installs the package within your Python environment which would be the likely virtualenv
/venv
if you are using it or the global environment otherwise.
For development however, this is not recommended. To test your code, you would need to either reinstall the package for every test or modify the files within the Python's site-packages
directory, which would mean it would be outside of your revision control system as well. That's where the development installs come in; instead of copying the package files to the Python package directory, they simply install a link within the site-packages
directory to the path where the package is actually located. This allows you to modify the code and immediately see the results in the scripts and applications you run without the need to reinstall your code after each change.
As is the case with a regular install, both pip
and setup.py
versions are available:
pip install –e package_directory
And the setup.py
version:
cd package_directory python setup.py develop