Python is a wonderful programming language and much more. One of its weakest points is packaging. This is a well-known fact in the community. Installing, importing, using and creating packages has improved over the years, but it's still not on par with newer languages like Go and Rust that could learn a lot from the struggles of Python and other more mature languages.
In this tutorial, you'll learn everything you need to know to build and share your own packages. For general background on Python packages, please read How to Use Python Packages.
Packaging a Project
Packaging a project is the process by which you take a hopefully coherent set of Python modules and possibly other files and put them in a structure that can be used easily. There are various things you have to consider, such as dependencies on other packages, internal structure (sub-packages), versioning, target audience, and form of package (source and/or binary).
A package's contents are typically stored in a single directory (although it is common to split sub-packages in multiple directories) and sometimes, as in this case, in its own git repository.
The root directory contains various configuration files (
setup.py is mandatory and the most important one), and the package code itself is usually in a subdirectory whose name is the name of the package and ideally a tests directory. Here is what it looks like for "conman":
> tree . ├── LICENSE ├── MANIFEST.in ├── README.md ├── conman │ ├── __init__.py │ ├── __pycache__ │ ├── conman_base.py │ ├── conman_etcd.py │ └── conman_file.py ├── requirements.txt ├── setup.cfg ├── setup.py ├── test-requirements.txt ├── tests │ ├── __pycache__ │ ├── conman_etcd_test.py │ ├── conman_file_test.py │ └── etcd_test_util.py └── tox.ini
Let's take a quick peek at the
setup.py file. It imports two functions from the setuptools package:
find_packages(). Then it calls the
setup() function and uses
find_packages() for one of the parameters.
from setuptools import setup, find_packages setup(name='conman', version='0.3', url='https://github.com/the-gigi/conman', license='MIT', author='Gigi Sayfan', email@example.com', description='Manage configuration files', packages=find_packages(exclude=['tests']), long_description=open('README.md').read(), zip_safe=False, setup_requires=['nose>=1.0'], test_suite='nose.collector')
This is pretty normal. While the
setup.py file is a regular Python file and you can do whatever you want in it, its primary job it to call the
setup() function with the appropriate parameters because it will be invoked by various tools in a standard way when installing your package. I'll go over the details in the next section.
The Configuration Files
In addition to
setup.py, there are a few other optional configuration files that can show up here and serve various purposes.
setup() function takes a large number of named arguments to control many aspects of package installation as well as running various commands. Many arguments specify metadata used for searching and filtering when uploading your package to a repository.
- name: the name of your package (and how it will be listed on PYPI)
- version: this is critical for maintaining proper dependency management
- url: the URL of your package, typically GitHub or maybe the readthedocs URL
- packages: list of sub-packages that need to be included;
- setup_requires: here you specify dependencies
- test_suite: which tool to run at test time
long_description is set here to the contents of the
README.md file, which is a best practice to have a single source of truth.
The setup.py file also serves a command-line interface to run various commands. For example, to run the unit tests, you can type:
python setup.py test
running test running egg_info writing conman.egg-info/PKG-INFO writing top-level names to conman.egg-info/top_level.txt writing dependency_links to conman.egg-info/dependency_links.txt reading manifest file 'conman.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'conman.egg-info/SOURCES.txt' running build_ext test_add_bad_key (conman_etcd_test.ConManEtcdTest) ... ok test_add_good_key (conman_etcd_test.ConManEtcdTest) ... ok test_dictionary_access (conman_etcd_test.ConManEtcdTest) ... ok test_initialization (conman_etcd_test.ConManEtcdTest) ... ok test_refresh (conman_etcd_test.ConManEtcdTest) ... ok test_add_config_file_from_env_var (conman_file_test.ConmanFileTest) ... ok test_add_config_file_simple_guess_file_type (conman_file_test.ConmanFileTest) ... ok test_add_config_file_simple_unknown_wrong_file_type (conman_file_test.ConmanFileTest) ... ok test_add_config_file_simple_with_file_type (conman_file_test.ConmanFileTest) ... ok test_add_config_file_simple_wrong_file_type (conman_file_test.ConmanFileTest) ... ok test_add_config_file_with_base_dir (conman_file_test.ConmanFileTest) ... ok test_dictionary_access (conman_file_test.ConmanFileTest) ... ok test_guess_file_type (conman_file_test.ConmanFileTest) ... ok test_init_no_files (conman_file_test.ConmanFileTest) ... ok test_init_some_bad_files (conman_file_test.ConmanFileTest) ... ok test_init_some_good_files (conman_file_test.ConmanFileTest) ... ok ---------------------------------------------------------------------- Ran 16 tests in 0.160s OK
The setup.cfg is an ini format file that may contain option defaults for commands you pass to
setup.py. Here, setup.cfg contains some options for
nosetests (our test runner):
[nosetests] verbose=1 nocapture=1
This file contains files that are not part of the internal package directory, but you still want to include. Those are typically the
readme file, the license file and similar. An important file is the
requirements.txt. This file is used by pip to install other required packages.
Here is conman's
include LICENSE include README.md include requirements.txt
You can specify dependencies both in the
install_requires section of
setup.py and in a
requirements.txt file. Pip will install automatically dependencies from
install_requires, but not from the
requirements.txt file. To install those requirements, you'll have to specify it explicitly when running pip:
pip install -r requirements.txt.
install_requires option is designed to specify minimal and more abstract requirements at the major version level. The requirements.txt file is for more concrete requirements often with pinned down minor versions.
Here is the requirements file of conman. You can see that all the versions are pinned, which means it can be negatively impacted if one of these packages upgrades and introduces a change that breaks conman.
PyYAML==3.11 python-etcd==0.4.3 urllib3==1.7 pyOpenSSL==0.15.1 psutil==4.0.0 six==1.7.3
Pinning gives you predictability and peace of mind. This is especially important if many people install your package at different times. Without pinning, each person will get a different mix of dependency versions based on when they installed it. The downside of pinning is that if you don't keep up with your dependencies development, you may get stuck on an old, poorly performing and even vulnerable version of some dependency.
I originally wrote conman in 2014 and didn't pay much attention to it. Now, for this tutorial I upgraded everything and there were some major improvements across the board for almost every dependency.
You can create a source distribution or a binary distribution. I'll cover both.
You create a source distribution with the command:
python setup.py sdist. Here is the output for conman:
> python setup.py sdist running sdist running egg_info writing conman.egg-info/PKG-INFO writing top-level names to conman.egg-info/top_level.txt writing dependency_links to conman.egg-info/dependency_links.txt reading manifest file 'conman.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'conman.egg-info/SOURCES.txt' warning: sdist: standard file not found: should have one of README, README.rst, README.txt running check creating conman-0.3 creating conman-0.3/conman creating conman-0.3/conman.egg-info making hard links in conman-0.3... hard linking LICENSE -> conman-0.3 hard linking MANIFEST.in -> conman-0.3 hard linking README.md -> conman-0.3 hard linking requirements.txt -> conman-0.3 hard linking setup.cfg -> conman-0.3 hard linking setup.py -> conman-0.3 hard linking conman/__init__.py -> conman-0.3/conman hard linking conman/conman_base.py -> conman-0.3/conman hard linking conman/conman_etcd.py -> conman-0.3/conman hard linking conman/conman_file.py -> conman-0.3/conman hard linking conman.egg-info/PKG-INFO -> conman-0.3/conman.egg-info hard linking conman.egg-info/SOURCES.txt -> conman-0.3/conman.egg-info hard linking conman.egg-info/dependency_links.txt -> conman-0.3/conman.egg-info hard linking conman.egg-info/not-zip-safe -> conman-0.3/conman.egg-info hard linking conman.egg-info/top_level.txt -> conman-0.3/conman.egg-info copying setup.cfg -> conman-0.3 Writing conman-0.3/setup.cfg creating dist Creating tar archive removing 'conman-0.3' (and everything under it)
As you can see, I got one warning about missing a README file with one of the standard prefixes because I like Markdown so I have a "README.md" instead. Other than that, all the package source files were included and the additional files. Then, a bunch of metadata was created in the
conman.egg-info directory. Finally, a compressed tar archive called
conman-0.3.tar.gz is created and put into a
Installing this package will require a build step (even though it's pure Python). You can install it using pip normally, just by passing the path to the package. For example:
pip install dist/conman-0.3.tar.gz Processing ./dist/conman-0.3.tar.gz Installing collected packages: conman Running setup.py install for conman ... done Successfully installed conman-0.3
Conman has been installed into site-packages and can be imported like any other package:
import conman conman.__file__ '/Users/gigi/.virtualenvs/conman/lib/python2.7/site-packages/conman/__init__.pyc'
Wheels are a relatively new way to package Python code and optionally C extensions. They replace the egg format. There are several types of wheels: pure Python wheels, platform wheels, and universal wheels. The pure Python wheels are packages like conman that don't have any C extension code.
The platform wheels do have C extension code. The universal wheels are pure Python wheels that are compatible with both Python 2 and Python 3 with the same code base (they don't require even 2to3). If you have a pure Python package and you want your package to support both Python 2 and Python 3 (becoming more and more important) then you can build a single universal build instead of one wheel for Python 2 and one wheel for Python 3.
If your package has C extension code, you must build a platform wheel for each platform. The huge benefit of wheels especially for packages with C extensions is that there is no need to have compiler and supporting libraries available on the target machine. The wheel already contains a built package. So you know it will not fail to build and it is much faster to install because it is literally just a copy. People that use scientific libraries like Numpy and Pandas can really appreciate this, as installing such packages used to take a long time and might have failed if some library was missing or the compiler wasn't configured properly.
The command to build pure or platform wheels is:
python setup.py bdist_wheel.
Setuptools—the engine that provides the
setup() function—will detect automatically if a pure or platform wheel is needed.
running bdist_wheel running build running build_py creating build creating build/lib creating build/lib/conman copying conman/__init__.py -> build/lib/conman copying conman/conman_base.py -> build/lib/conman copying conman/conman_etcd.py -> build/lib/conman copying conman/conman_file.py -> build/lib/conman installing to build/bdist.macosx-10.9-x86_64/wheel running install running install_lib creating build/bdist.macosx-10.9-x86_64 creating build/bdist.macosx-10.9-x86_64/wheel creating build/bdist.macosx-10.9-x86_64/wheel/conman copying build/lib/conman/__init__.py -> build/bdist.macosx-10.9-x86_64/wheel/conman copying build/lib/conman/conman_base.py -> build/bdist.macosx-10.9-x86_64/wheel/conman copying build/lib/conman/conman_etcd.py -> build/bdist.macosx-10.9-x86_64/wheel/conman copying build/lib/conman/conman_file.py -> build/bdist.macosx-10.9-x86_64/wheel/conman running install_egg_info running egg_info creating conman.egg-info writing conman.egg-info/PKG-INFO writing top-level names to conman.egg-info/top_level.txt writing dependency_links to conman.egg-info/dependency_links.txt writing manifest file 'conman.egg-info/SOURCES.txt' reading manifest file 'conman.egg-info/SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'conman.egg-info/SOURCES.txt' Copying conman.egg-info to build/bdist.macosx-10.9-x86_64/wheel/conman-0.3-py2.7.egg-info running install_scripts creating build/bdist.macosx-10.9-x86_64/wheel/conman-0.3.dist-info/WHEEL
dist directory, you can see that a pure Python wheel was created:
ls -la dist dist/ total 32 -rw-r--r-- 1 gigi staff 5.5K Feb 29 07:57 conman-0.3-py2-none-any.whl -rw-r--r-- 1 gigi staff 4.4K Feb 28 23:33 conman-0.3.tar.gz
The name "conman-0.3-py2-none-any.whl" has several components: package name, package version, Python version, platform version, and finally the "whl" extension.
To build universal packages, you just add
--universal, as in
python setup.py bdist_wheel --universal.
The resulting wheel is called "conman-0.3-py2.py3-none-any.whl".
Note that it is your responsibility to ensure your code actually works under both Python 2 and Python 3 if you create a universal package.
Writing your own Python packages requires dealing with a lot of tools, specifying a lot of metadata, and thinking carefully about your dependencies and target audience. But the reward is great.
If you write useful code and package it properly, people will be able to install it easily and benefit from it.
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post