Skip to content

Lesson 2 of 9 · 22m

pyproject.toml That Scales

What belongs in pyproject.toml, how optional dependencies and entry points work, and the metadata mistakes that cause real-world breakage.

pyproject.toml is not merely the replacement for setup.py; it is the project's packaging contract. Done well, it makes builds reproducible and tells installers exactly what they need. Done badly, it becomes a junk drawer of half-remembered settings copied from three tutorials.

A production-shaped starting point

[build-system]
requires = ["hatchling>=1.21"]
build-backend = "hatchling.build"

[project]
name = "acme-metrics"
version = "1.4.2"
description = "Metrics primitives for Python services"
readme = "README.md"
requires-python = ">=3.10"
license = { text = "MIT" }
authors = [{ name = "Acme Platform", email = "platform@acme.dev" }]
dependencies = [
  "typing-extensions>=4.8; python_version < '3.12'",
]

[project.optional-dependencies]
redis = ["redis>=5.0,<6"]
cli = ["rich>=13.7", "typer>=0.12"]

Static metadata is the default because it is predictable. name, version, requires-python, dependencies, and extras should be visible without running Python. Dynamic metadata belongs behind a deliberate decision, not because copying a snippet was easier.

Extras are optional capabilities, not environments

An extra answers: what additional dependencies activate this optional feature? Users install them with square brackets:

pip install 'acme-metrics[redis,cli]'

Do not put development tools — pytest, ruff, mypy — in extras unless end users truly install them. Development requirements belong in a lockfile, dependency group, or separate requirements file, depending on your workflow.

Entry points create public surfaces

[project.scripts]
acme-metrics = "acme_metrics.cli:main"

[project.entry-points."acme.plugins"]
sqlite = "acme_metrics.backends.sqlite:SQLiteBackend"

Console scripts are executable contracts. Plugin entry points are discovery contracts. Both are installed metadata, which means renaming a function or module is a breaking packaging change even if the Python API still imports.

License and classifiers matter

Modern backends increasingly support SPDX license expressions such as license = "MIT"; older ones may still expect the table form, so check before modernizing. Classifiers are mostly informational, but incorrect requires-python is not — it determines which environments pip will even attempt to install into.

Field notes

  • Quote dependency markers containing < or version expressions; TOML parsing errors here are common.
  • The project name is the distribution name on PyPI, not necessarily the import package name. scikit-learn installs sklearn.
  • Validate locally with python -m build and twine check dist/* before publishing.
  • Keep tool config in pyproject.toml only when the tool explicitly supports it; otherwise use the tool's native config file.

Was this lesson helpful?

Sign in to rate