Originally posted on opensource.
Use setuptools to deliver Python code to users.
You’ve spent weeks perfecting your code. You’ve tested it and sent it to some close developer friends for quality assurance. You’ve posted all the source code on your personal Git server, and you’ve received helpful bug reports from a few brave early adopters. And now you’re ready to make your Python code available to the world.
Delivering code to its target is a big deal. It’s a whole branch of software development, it’s the “D” in CI/CD, and yet many people forget all about, at least until the end. I’ve written articles about Autotools and Cmake, but some languages have their own methods to help you make your code readily available to users. For Python, a common way to deliver code to users is with
The easiest way to install and update
setuptools is with
$ sudo python -m pip install --upgrade setuptools
Create a simple Python library called
myhellolib for some example code in need of packaging. This library accepts a string and then prints the string in capital letters.
It’s two lines of code, but project structure is important, so first create the directory tree:
$ mkdir -p myhellolib.git/myhellolib
To confirm that this project is an importable library (a Python “module”), create the empty file
__init__.py in the code directory, along with the file that contains the code:
$ touch myhellolib.git/myhellolib/myhellolib.py
myhellolib.py file, enter this simple Python code:
That’s the library written.
Before packaging it up, test your library. Create a
myhellolib.git/test.py file and enter this code:
Run the script:
$ python ./test.py
It works, so now you can package it up.
To package a project with
setuptools, you must create a
.toml file identifying
setuptools as the build system. Place this text in a file called
myhellolib.toml in your project directory:
requires = [“setuptools”, “wheel”]
build-backend = “setuptools.build_meta”
Next, create a file called
setup.py, containing metadata about your project:
‘importlib; python_version == “3.8”‘,
Believe it or not, that’s all the setup
setuptools requires. Your project is ready for packaging.
To create your Python package, you need a builder. A common tool is
build, which you can install with
$ python -m pip install build --user
Build your project:
$ python -m build
After a few moments, the build completes, and there’s a new directory in your project folder called
dist. This folder contains a
.tar.gz and a
Your very first Python package! Here’s what each one contains:
$ unzip -l dist/myhellolib-0.0.1-py3-none-any.whl
Making it available
Now that you know how easy it is to package up your Python package, you can either 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
build for this article!
It’s not often the first thing you think about when sitting down to develop an application or library, but packaging code is an important aspect of programming. Python developers put a lot of thought into how programmers can make their work available to the world, and it doesn’t get much easier than
setuptools. Try it out, use it, and keep coding in Python!