How to package Python code

In this article, I will show you how to package Python code. You spent weeks perfecting your code. You have tested it and sent it to testers for quality assurance. You’ve posted all the source code to your personal Git server, and you’ve received helpful bug reports from some of the early users. And now you’re ready to give your Python code to the world.

How to package your Python code

And that’s when you realise. You do not know how to deliver the product. Providing code to people in need is a big deal. It’s an entire branch of software development, that’s the “D” in CI/CD, but many people forget. You can use Autotools and Cmake, but some other languages ​​have their own methods to help you make your code available to users. For Python, a common way to provide code to users is to use setuptools.

The easiest way to install and update setuptools is to use pip:

sudo python -m pip install --upgrade setuptools

Create an example library

Created a simple Python library called MyHellolib for some example code that needed packaging. This library takes a string and then prints the string in uppercase.

It’s two lines of code, but the project structure is important, so let’s create the directory tree first:

mkdir -p myhellolib.git/myhellolib

To confirm that this project is an importable library (a Python “module”), create an empty file __init__.py in the code directory, along with the file containing the code:

touch myhellolib.git/myhellolib/__init__.py
touch myhellolib.git/myhellolib/myhellolib.py

In the myhellolib.py file, enter the following simple Python code:

def greeter(s):
    print(s.upper())

That’s the library part.

Check out the library

Before packaging it, check your library. Create the file myhellolib.git/test.py and enter this code:

import myhellolib.myhellolib as hello

hello.greeter("Hello Opensource.com.")

Run the script:

$ cd myhellolib.git
$ python ./test.py
HELLO OPENSOURCE.COM

It works, so now you can pack it.

To package a project with setuptools, you must create a .toml file that defines setuptools as the build system. Put this in a file called myhellolib.toml in your project directory:

[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

Next, create a file called setup.py, which contains information about your project:

from setuptools import setup

setup(
    name="myhellolib",
    version='0.0.1',
    packages=['myhellolib'],
    install_requires=[
        'requests',
        'importlib; python_version == "3.8"',
    ],
)

That’s all setuptools requires. Your project is ready to be packaged.

Code packaging

To create your Python package, you need a builder. One popular tool is i, which you can install using pip:

python -m pip install build --user

Build your project:

python -m build

After a few minutes the build is complete and there is a new folder in your project folder called dist. This folder contains .tar.gz and .whl files.

Here’s your first Python package:

$ tar --list --file dist/myhellolib-0.0.1.tar.gz
myhellolib-0.0.1/
myhellolib-0.0.1/PKG-INFO
myhellolib-0.0.1/myhellolib/
myhellolib-0.0.1/myhellolib/__init__.py
myhellolib-0.0.1/myhellolib/myhellolib.py
myhellolib-0.0.1/myhellolib.egg-info/
myhellolib-0.0.1/myhellolib.egg-info/PKG-INFO
myhellolib-0.0.1/myhellolib.egg-info/SOURCES.txt
myhellolib-0.0.1/myhellolib.egg-info/dependency_links.txt
myhellolib-0.0.1/myhellolib.egg-info/requires.txt
myhellolib-0.0.1/myhellolib.egg-info/top_level.txt
myhellolib-0.0.1/setup.cfg
myhellolib-0.0.1/setup.py

$ unzip -l dist/myhellolib-0.0.1-py3-none-any.whl 
Archive:  dist/myhellolib-0.0.1-py3-none-any.whl
Name
----
myhellolib/__init__.py
myhellolib/myhellolib.py
myhellolib-0.0.1.dist-info/METADATA
myhellolib-0.0.1.dist-info/WHEEL
myhellolib-0.0.1.dist-info/top_level.txt
myhellolib-0.0.1.dist-info/RECORD
-------
6 files

Upload the packaged code

Now that you know how easy it is to package your Python code, you can automate the process using Git hooks, GitLab webhooks, Jenkins, or a similar automation tool. You can even upload your project to PyPi, the popular repository for Python modules. Once it’s on PyPi, users can install it using pip, the same way you installed setuptools and build.

This is not the first thing that comes to mind when developing an application or library, but coding is an important aspect of programming. And I think setuptools is the simplest way to package your Python code.

Leave a Reply