Skip to main content

How to Write Your Own Python Packages

Image result for python

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 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).
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 thesetuptools 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.
In addition to setup.py, there are a few other optional configuration files that can show up here and serve various purposes.
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.
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 fornosetests (our test runner):
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:
You can specify dependencies both in the install_requires section of setup.pyand 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.
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:
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 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.
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. 

Comments

You may also want to read these ⤵️

Referee kills player in a football match

A referee is facing murder charges after football players allegedly forced him to

Do not watch this while driving

Kids are lovely and fun to watch most times. I know most of you did this and so many other funny stuffs as a kid. Feel free to share yours... Do not watch this while driving

The Pros and Cons Of COVID-19 Contact Tracing Apps

                Written by                Jack -  Guest author Contact tracing apps seem to be the new fad. Their popularity is also one that comes on the back of a series of unfortunate events. They seem to hold another approach to fighting the menace of a disease that has claimed no less than 400,000 lives worldwide. The widespread race to get these apps working can also be attributed to the coming together of two big names in tech, Apple and Google, to make the frameworks for such apps happen. Like every other thing, though, how does the scales tip in favor of, or against, these apps? The Case for Contact Tracing Apps In April alone, the US people lost more jobs than the economy had been able to gain in about a combined decade. More unemployment claims are filed daily, while companies do not take their staff out of furlough anytime soon. Businesses are folding up daily, too, especially those still p...

Over 40 Million Accounts Found Guilty

Microsoft has uncovered 44 million user accounts using usernames and passwords that have been leaked through security breaches.

These 10 Powerful Words And Phrases Defined The Decade

Honestly, it has been a wonderful decade to remember.  A lot has happened and a lot has been spoken also. But our focus is on the words and phrases spoken.  Below are words and phrases spoken between 2010 - 2019 that defined the decade.....

RAW TALENT ep1 (freestyle by Gdlpeid)

Just watch! Freestyle by ''Gdlpeeid''. A rapper with a difference.  Pure raw talent.

By February 2020 - WhatsApp Will Stop Working on These Phones

Every now and then, WhatsApp does fish out a list of old phones for which support is discontinued and if you have an old phone lying around as a backup, you might want to read on.

Apple Has Released iOS 13.2.2 And Fixes Major Issue

All thanks to Apple,  the tech  giant just released iOS 13.2.2, which addresses the issue of background apps being killed prematurely, along with a handful of other annoyances.

This Magnetic thread Can Be Used To Clear Blood Clot in The Brain

Link from mashable.com  Researchers at MIT developed a thread that can be steered magnetically to glide through the brain's blood vessels and

This gigantic monster device turns wave energy into electricity

This 826-ton buoy was developed by OceanEnergy to turn wave energy into electricity. IEEE Spectrum reported that "OE Buoy" was towed from Oregon to Hawaii, where it will undergo a series of tests that will prove whether it can withstand the battering waves while generating electricity. Click the link below to watch the video..