TExcelTAD
Home › Python Packaging: pip, venv, Poetry & uv › pyproject.toml: PEP 517, 518 & 621

pyproject.toml: PEP 517, 518 & 621

For twenty years, building a Python package meant running setup.py, a Python script that imported setuptools and did whatever it liked. Tools couldn't read a project's name or dependencies without executing arbitrary code, and you couldn't build with anything except setuptools. Three PEPs replaced that with one declarative file, pyproject.toml, and a clean split between front ends (pip, build, uv, Poetry) and back ends (setuptools, hatchling, poetry-core, flit-core, uv_build).

The three tables

toml
# 1. PEP 518: what you need installed to build this project, and (PEP 517) which backend to call
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

# 2. PEP 621: the project's metadata, readable by every tool
[project]
name = "tally"
version = "1.2.0"
description = "Count things"
readme = "README.md"
requires-python = ">=3.13"
license = "MIT"                       # an SPDX expression (PEP 639)
authors = [{ name = "Ada Lovelace", email = "ada@example.com" }]
dependencies = ["attrs>=24", 'colorama; sys_platform == "win32"']

[project.optional-dependencies]       # "extras": pip install tally[cli]
cli = ["rich>=13"]

[project.scripts]                     # console commands
tally = "tally.cli:main"

[project.urls]
Homepage = "https://example.com"

# 3. Tool-specific settings live under [tool.<name>]
[tool.hatch.version]
path = "src/tally/__init__.py"
[tool.pytest.ini_options]
testpaths = ["tests"]
PEP Year What it standardised
518 2016 the file itself and [build-system] requires: build dependencies are installed before building, in isolation
517 2017 build-backend: a Python module with hook functions (build_wheel, build_sdist, …) that any front end can call
621 2020 the [project] table: name, version, dependencies, scripts, in a format every backend reads the same way
660 2021 the build_editable hook for pip install -e
639 2024 license = "MIT" as an SPDX expression, plus license-files
735 2024 [dependency-groups]: dev/test dependencies that are not published (Poetry 2 and uv write them)

A backend is just a module

PEP 517 is refreshingly simple: the front end imports the module named in build-backend and calls functions on it. There's no magic in pip or python -m build; you can play front end yourself:

bash · try it yourself
python3 -m venv .venv && . .venv/bin/activate
pip install -q hatchling==1.32.4
mkdir -p tally/src/tally && cd tally
cat > pyproject.toml <<'TOML'
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "tally"
version = "1.0.0"
requires-python = ">=3.13"
TOML
echo 'def count(xs): return len(xs)' > src/tally/__init__.py
python - <<'PY'
import hatchling.build as backend          # what build-backend names
hooks = [n for n in dir(backend) if n.startswith(("build_", "get_requires", "prepare_"))]
print("hooks:", ", ".join(hooks))
print("extra build deps:", backend.get_requires_for_build_wheel())
print("built:", backend.build_wheel("dist"))
print("built:", backend.build_sdist("dist"))
PY
hooks: build_editable, build_sdist, build_wheel, get_requires_for_build_editable, get_requires_for_build_sdist, get_requires_for_build_wheel, prepare_metadata_for_build_editable, prepare_metadata_for_build_wheel
extra build deps: []
built: tally-1.0.0-py3-none-any.whl
built: tally-1.0.0.tar.gz

What a real front end adds around those calls is build isolation: it creates a temporary environment, installs [build-system] requires into it (plus whatever get_requires_for_build_wheel returns), and runs the hooks there, so your build doesn't depend on whatever happens to be installed on the machine.

Metadata ends up in METADATA

Every [project] field turns into a line of the wheel's METADATA file (the "core metadata" format, RFC 822 headers). This is what pip reads to resolve dependencies, and what PyPI shows on the project page. The version here is dynamic: listed in dynamic and read by hatchling from the package's __version__:

bash · try it yourself
python3 -m venv .venv && . .venv/bin/activate
pip install -q build
mkdir -p tally/src/tally && cd tally
cat > pyproject.toml <<'TOML'
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "tally"
dynamic = ["version"]
requires-python = ">=3.13"
license = "MIT"
dependencies = ["attrs>=24", 'colorama; sys_platform == "win32"']

[project.optional-dependencies]
cli = ["rich>=13"]

[project.urls]
Homepage = "https://example.com"

[tool.hatch.version]
path = "src/tally/__init__.py"
TOML
printf '__version__ = "1.2.0"\ndef count(xs): return len(xs)\n' > src/tally/__init__.py
echo "MIT License" > LICENSE
python -m build --wheel 2>&1 | tail -1
unzip -p dist/tally-1.2.0-py3-none-any.whl tally-1.2.0.dist-info/METADATA
Successfully built tally-1.2.0-py3-none-any.whl
Metadata-Version: 2.5
Name: tally
Version: 1.2.0
Project-URL: Homepage, https://example.com
License-Expression: MIT
License-File: LICENSE
Requires-Python: >=3.13
Requires-Dist: attrs>=24
Requires-Dist: colorama; sys_platform == 'win32'
Provides-Extra: cli
Requires-Dist: rich>=13; extra == 'cli'

Note how the extra became Provides-Extra: cli plus a Requires-Dist with the marker extra == 'cli', and the license file was copied into the wheel automatically.

Invalid metadata fails the build

Backends validate [project] strictly. Forget the version (and don't declare it dynamic) and the build stops:

bash · try it yourself
python3 -m venv .venv && . .venv/bin/activate
pip install -q build
mkdir -p tally/src/tally && cd tally
printf '[build-system]\nrequires = ["hatchling"]\nbuild-backend = "hatchling.build"\n\n[project]\nname = "tally"\n' > pyproject.toml
touch src/tally/__init__.py
python -m build --wheel 2>&1 | grep -E 'Error|ERROR'
    raise ValueError(message)
ValueError: Field `project.version` can only be resolved dynamically if `version` is in field `project.dynamic`
ERROR Backend subprocess exited when trying to invoke build_wheel

Tip: [project] is the portable part: move from setuptools to hatchling (or to uv_build) and it stays the same. Only [build-system] and the backend's [tool.*] table change.

Test yourself

What does [build-system] requires list?

  1. The project's runtime dependencies
  2. The packages a front end must install into the isolated build environment before calling the backend
  3. Development tools like pytest
  4. Supported Python versions

Under PEP 517, how does pip build a wheel with hatchling?

  1. It runs python setup.py bdist_wheel
  2. It imports the module named in build-backend (hatchling.build) in an isolated environment and calls its build_wheel hook
  3. It asks PyPI to build it
  4. It shells out to hatch build

Where do development-only dependencies (pytest, mypy) belong in a modern pyproject.toml?

  1. [project] dependencies
  2. [build-system] requires
  3. [dependency-groups] (PEP 735), or a tool's own group table
  4. [project.urls]