Packaging Python Projects
It is a short note to show how to package a simple Python project, build it and how to upload it to the Python Package Index, which is a repository of software for the Python programming language. PyPI helps you find and install software developed and shared by the Python community.
There is also Test Python Package Index, which is similar to PyPI but is a stage zone, where people can test their packages before uploading them to the official PyPI.
Project structure
The Python project should include necessary files and be structured to allow build
creating a package out of it.
Let’s begin to structure our project like this. Reference to samplepackage
setup.py
This is a heart of packaging our Python project as it contains metadata and drives the build
The above was an example from one of mine packages and it is quite self-explanatory. There are some notes here:
-
install_requires
contains all required packages or dependencies. It is important to lock in specific versions so your code will not break. -
entry_points
orconsole_scripts
. While you can do much more withconsole_scripts
, this allows specifying the entry point to your “executable” program. In this example, user can invokesamplepackage
which is equivalent tofoo.bar.main:main
. To further explain, we havefoo
as a packagebar
as another package- finally
main:main
is the main entry
.pypirc
This file resides at ~/.pypirc
containing all authentication needed to identify with pypi.org or test.pypi.org. One needs to create accounts on the two sites and fills in the .pyirc
with account details. There are also ways to authenticate using token, but I don’t find it particularly useful if you build code from your local machine.
Building package
List of commands
Uploading to PyPI or Test PyPI
Install twine, which is a utility for publishing Python packages on PyPI. It provides build system independent uploads of source and binary distribution artifacts.
Upload
After that, you should be able to view your newly uploaded package at https://pypi.org/ or https://test.pypi.org/