the full guide

Packaging and publishing

Packaging turns your code into something others can install with pip. A pyproject.toml file describes the project and its dependencies. Tools such as build create a wheel file, and twine uploads it to PyPI, the public package index.

8 min Advanced

What you need: The earlier topics of this module, and its prerequisites.

Definition

Packaging turns your code into something others can install with pip. A pyproject.toml file describes the project and its dependencies. Tools such as build create a wheel file, and twine uploads it to PyPI, the public package index.

In simple words

It's the difference between emailing someone a folder of code and letting them type pip install yourtool.

Why it matters

Even inside a company, packaging lets teams share code cleanly, with fixed versions. It's also a good way to show your work publicly.

How it works

my_tool/
    pyproject.toml
    README.md
    src/my_tool/__init__.py

# pyproject.toml
[project]
name = "my-tool"
version = "0.1.0"
dependencies = ["numpy>=1.26"]

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

# In the terminal
python -m build
python -m twine upload dist/*

Common mistakes

  • Publishing secrets by mistake. Check what goes into your package before uploading.
  • Not pinning versions for an application, so a library update breaks it.

Check yourself

Question 1

1. Which file describes a modern Python package?

Question 2

2. You fix a small bug in version 1.4.2. What's the new version?

Summary

  • pyproject.toml describes the package.
  • build makes it, and twine uploads it to PyPI.
  • Use semantic versions.

Finished reading?

Sources: Python Packaging User Guide