Python Packaging


In Python, the term packaging refers to putting modules you have written in a standard format, so that other programmers can install and use them with ease.
This involves use of the modules setuptools and distutils.
The first step in packaging is to organize existing files correctly. Place all of the files you want to put in a library in the same parent directory. This directory should also contain a file called, which can be blank but must be present in the directory.
This directory goes into another directory containing the readme and license, as well as an important file called
Example directory structure:



The next step in packaging is to write the file.
This contains information necessary to assemble the package so it can be uploaded to PyPI and installed with pip (name, version, etc.).
Example of a file:

from distutils.core import setup


After creating the file, upload it to PyPI, or use the command line to create a binary distribution (an executable installer).
To build a source distribution, use the command line to navigate to the directory containing, and run the command python sdist.
Run python bdist or, for Windows, python bdist_wininst to build a binary distribution.
Use python register, followed by python sdist upload to upload a package.

Finally, install a package with python install.


The previous lesson covered packaging modules for use by other Python programmers. However, many computer users who are not programmers do not have Python installed. Therefore, it is useful to package scripts as executable files for the relevant platform, such as the Windows or Mac operating systems. This is not necessary for Linux, as most Linux users do have Python installed, and are able to run scripts as they are.

For Windows, many tools are available for converting scripts to executables. For example, py2exe, can be used to package a Python script, along with the libraries it requires, into a single executable.
PyInstaller and cx_Freeze serve the same purpose.

For Macs, use py2appPyInstaller or cx_Freeze.

Leave a Comment