A Six-Part Self-Study Course on Python Packaging

This outline takes you from a folder of Python files to a library someone else can pip install with confidence. It leans on the official Python Packaging User Guide at packaging.python.org, which is maintained by the Python Packaging Authority, and points you to the relevant PEPs rather than restating them. Expect roughly an evening per part.

Part 1: Layout

Learning goal: choose a project layout and know why.

The guide discusses both the "flat" layout and the src/ layout. The src/ layout puts your import package one level down, which stops tests from accidentally importing the working copy instead of the installed package. For a library you intend to publish, that safety is usually worth the extra folder.

package_d/
  pyproject.toml
  README.md
  LICENSE
  src/
    package_d/
      __init__.py
  tests/

Reading: "src layout vs flat layout" in the Python Packaging User Guide.

Part 2: Metadata in pyproject.toml

Learning goal: fill in the [project] table correctly.

PEP 621 standardised how core metadata is declared in pyproject.toml. A minimal library looks like this:

[project]
name = "package-d"
version = "0.3.0"
description = "Parses widget files."
readme = "README.md"
requires-python = ">=3.9"
license = "MIT"
dependencies = ["attrs>=23"]

[project.urls]
Source = "https://example.org/package-d"

Give requires-python real thought: it is what stops pip installing your package on interpreters you never tested. Express dependencies as lower bounds unless you know of an incompatibility; tight upper pins in libraries cause resolver conflicts for your users.

Reading: PEP 621, and the "Writing your pyproject.toml" page of the guide.

Part 3: Build backends

Learning goal: understand the frontend/backend split.

Since PEP 517 and PEP 518, tools such as pip and build are frontends that call a backend named in the [build-system] table. Common backends include setuptools, Hatchling, Flit and PDM's backend. They all produce the same artefact types, an sdist and a wheel. Pick one that supports the metadata features you need and stop there; switching later is easy if your metadata lives in [project].

Part 4: Version numbers

Learning goal: produce versions that pip sorts correctly.

Python versions follow PEP 440, which is close to Semantic Versioning but not identical. Pre-releases are written 1.0.0rc1 or 1.0.0b2, development releases 1.0.0.dev3, and post-releases 1.0.0.post1. Hyphenated semver pre-release strings get normalised, so write the PEP 440 form directly to avoid surprises.

Exercise: sort these by hand, then check with the packaging library: 1.0.0, 1.0.0rc1, 1.0.0.dev1, 1.0.0.post1.

Part 5: Test what you ship

Learning goal: run your test suite against the built wheel.

  1. Run python -m build to create dist/.
  2. Create a fresh virtual environment and install the wheel from dist/.
  3. Run the tests from outside the source tree.
  4. Run twine check dist/* to validate the README renders on the package index.

Tools like nox or tox can automate this across several Python versions.

Part 6: Upload

Learning goal: publish safely, once.

Practise on TestPyPI first; it is a separate index meant for exactly this. For the real upload, prefer PyPI Trusted Publishing from CI over a personal API token. Our trusted publishing tutorial walks through the workflow. Remember that PyPI does not let you reuse a version number for a different file, even after deleting a release, so a mistake means a new version.

Checklist for finishing the course