Sub topic 2
pyproject.toml: one file that describes the project
This chapter assumes you can write Python packages, use a virtual environment and pip, and read basic TOML. By the end you will know what the three tables of pyproject.toml do and how to spot the usual mistakes.
Why one file
Older projects spread configuration across setup.py, setup.cfg, MANIFEST.in, and tool-specific files. setup.py was executable code, so a tool had to run it just to learn the project's name. pyproject.toml replaces that with a static, declarative file. Any tool can read it without running your code.
The file is TOML, a format of [tables] containing key = value pairs. Packaging uses up to three top-level tables.
| Table | Purpose | Read by |
|---|---|---|
| `[build-system]` | Which tool builds the package | Build frontends such as pip and `build` |
| `[project]` | Name, version, dependencies, entry points | Build backend, PyPI, pip |
| `[tool.*]` | Settings for individual tools | The named tool, such as pytest or ruff |
The build-system table
This table is the only one a frontend reads before it builds anything. It names the packages needed to build and the backend entry point to call.
[build-system]
requires = ["setuptools>=77"]
build-backend = "setuptools.build_meta"requires is installed into a temporary, isolated environment. build-backend is the Python object that produces the distribution files. The next chapter covers what happens next.
The project table
This table holds the metadata of the library.
[project]
name = "greeter"
version = "0.1.0"
description = "A tiny library that builds greeting messages."
readme = "README.md"
requires-python = ">=3.9"
dependencies = []Later chapters go through each field. For now, note that the keys are standardized. Every backend reads [project] the same way, so you can switch backends without rewriting your metadata.
The tool tables
Anything under [tool.<name>] belongs to that tool. Packaging tools ignore tables they do not own.
[tool.pytest.ini_options]
testpaths = ["tests"]How the pieces connect
Common errors
- Missing
[build-system]. pip falls back to legacy setuptools behavior. Always declare it, so builds are the same on every machine. - Invalid TOML. Strings need double quotes, and lists need commas. A parse error stops the whole build.
- Name and version declared twice. If a field appears in
[project]and a backend section, the two can disagree. Keep each fact in one place. - A field the backend does not support. Newer keys, such as
license = "MIT"as an SPDX string, need a recent backend. This is why the example requiressetuptools>=77.
Exercise
A teammate's build fails with "Cannot import 'setuptools.build_meta'" on a clean machine, but works on theirs. Which table do you check first, and why?
Show answer
Check [build-system]. Its requires list defines what is installed in the isolated build environment. If the backend or its minimum version is missing there, the build only works on machines that happen to have it installed already.
Exercise
Why is a static pyproject.toml better than running setup.py to learn a project's metadata?
Show answer
A tool can read the file without executing project code. This is faster, safer, and gives the same answer in every environment.