# How to Write Your Own Python Packages

## Overview

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).

### Example

Let's start with a quick example. The conman package is a package for managing configuration. It supports several file formats as well as distributed configuration using etcd.

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":

Let's take a quick peek at the setup.py file. It imports two functions from the setuptools package: setup() and find_packages(). Then it calls the setup() function and uses find_packages() for one of the parameters.

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.py

The 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; find_packages() helps here
• setup_requires: here you specify dependencies
• test_suite: which tool to run at test time

The 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.

### Setup.cfg

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

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):

### MANIFEST.in

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 MANIFEST.in file:

### Dependencies

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.

The 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.

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.

## Distributions

You can create a source distribution or a binary distribution. I'll cover both.

### Source Distribution

You create a source distribution with the command: python setup.py sdist. Here is the output for conman:

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 dist sub-directory.

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:

Conman has been installed into site-packages and can be imported like any other package:

## Wheels

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.

Checking the dist directory, you can see that a pure Python wheel was created:

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.

## Conclusion

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.