Python - Packaging & Distribution
Overview
Estimated time: 45–60 minutes
Package your project with pyproject.toml, build wheels, version consistently, and publish safely to PyPI with basic supply-chain safeguards.
Learning Objectives
- Structure a project with src-layout and pyproject.toml.
- Build wheels and perform editable installs.
- Publish to TestPyPI/PyPI with tokens; understand constraints and hashes.
Prerequisites
Minimal pyproject.toml (setuptools)
[build-system]
requires = ["setuptools>=68", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "my_package"
version = "0.1.0"
description = "Example"
authors = [{name="Your Name"}]
readme = "README.md"
requires-python = ">=3.11"
Build & install
pip install build
python -m build
pip install -e .
Publish
pip install twine
# Upload to TestPyPI first
python -m twine upload --repository testpypi dist/*
Common Pitfalls
- Publishing without testing install from a clean environment.
- Missing long_description/README causing poor package page rendering.
Checks for Understanding
- Why build wheels instead of source-only dists?
- What repository should you target for trial uploads?
Show answers
- Faster installs and prebuilt artifacts for users; avoids building at install time.
- TestPyPI.
Exercises
- Convert a simple project to pyproject.toml and build a wheel.
- Upload to TestPyPI and install from a fresh virtual environment.