Lesson · Virtual Environments, Dependencies & Packaging
pyproject.toml
65 min readpyproject.toml
`pyproject.toml` is the central configuration file for modern Python packaging and many development tools. Understanding its build-system and project metadata sections removes much packaging mystery.
Learning goals
- Read a
pyproject.tomland tell its two essential tables apart:[build-system]and[project]. - Explain what
requiresandbuild-backendunder[build-system]promise the tool that builds the project. - Name the core
[project]fields (name,version,requires-python,dependencies) and what each one controls. - Say where tool-specific settings live in the same file, and why they share it instead of scattering config files.
- Describe which of these declarations travel with the built distribution as metadata.
Mental model
The file answers three different questions in one place: how the project is built, what it is, and how the supporting tools behave. The [build-system] table is consulted before anything else, because the backend it names is the code that reads the rest.
Mental model
read it top to bottom
[build-system] how to build: build-backend plus requires ↓ the front end (pip, build) installs the backend and hands it the project [project] what the project is: name, version, requires-python ↓ the backend produces wheel and sdist with matching metadata [tool.*] sections configuration for formatters, testers, linters
Core idea
pyproject.toml moves project facts out of code and into declarations: a build backend for the how, [project] metadata for the what, and tool sections for the surrounding workflow. The build system is the hinge, because the backend named there is what turns editable source into a wheel or sdist, and a front-end tool installs that backend rather than trusting the project's own build logic. Keeping all of it in one declarative file is why modern packaging can be tool-agnostic.
Project metadata
In pyproject.toml, Project metadata names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Build-system configuration
In pyproject.toml, Build-system configuration names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Dependencies
The dependencies list under [project] names the distributions the project needs at run time, each written as a requirement string such as requests>=2 or requests==<version>. Front-end tools read the list when installing, so it is the project's promise about what its environment must contain; packages used only for development are recorded separately, as an optional-dependency extra or a dependency group.
Tool configuration
In pyproject.toml, Tool configuration names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Modern packaging workflow
In pyproject.toml, Modern packaging workflow names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Walkthrough
[build-system]nameshatchlinginrequiresandhatchling.buildas the backend; a front-end tool installs that requirement first, then delegates the actual build to the named backend.[project]declares the distribution's identity:name = "acme-tools"becomes the installable name,version = "1.0.0"the release, andrequires-python = ">=3.12"the interpreter range the package promises to support.dependencies = []is the run-time requirement list; keeping it empty makes this example minimal, so an install pulls in nothing beyond the package itself.- Predict before running: which table a build tool reads first, and which field would change if the package needed one more library at run time.
Experiments to try
- Add one requirement to
dependencies(for example"requests>=2"), build the project, and find the line in the generated metadata that repeats it. - Set
requires-pythonto a range your interpreter does not satisfy, try to install the built wheel, and read the refusal message. - Change
nameand thenversionand check which value appears on the built artifact's filename. - Add a
[tool.]section for a formatter of your choice and confirm the tool reads it from this file instead of a separate config file.
Common mistakes
- Putting build logic in a
setup.pywhile[build-system]names something else: the backend declared in the file wins, and the hand-written script quietly stops being consulted. - Forgetting
requires: the backend cannot be installed before the build starts, so the front end fails with an error about a missing build dependency it was supposed to fetch from the list. - Writing
requires-pythonto match your machine instead of the versions you actually test: the claim travels in the metadata, and installers on excluded versions refuse the package. - Scattering tool configuration across
setup.cfg,.flake8and other files: readers then hunt for settings in several places, which is exactly the ambiguitypyproject.tomlwas meant to end.
Practice: Easy
- Copy the worked example into a new file, remove the
[project]table, and build; read the error that names the missing metadata and put the table back. - Change
dependenciesto include one library, rebuild, and find the matching requirement line inside the built metadata. - Add a
[tool.]section for one tool you use, run that tool, and confirm it stops looking for its old config file.
Practice: Medium
- Take a project that still declares its build with
setup.pyand move the equivalent fields into[project]; show that the build output is unchanged. - Build the project twice, once with
requires-pythonset to your interpreter and once to an impossible range, and compare what each artifact claims. - Add a second
[tool.]section for a different tool and explain why both configurations can share the file without interfering.
Practice: Hard
- Design the
pyproject.tomlfor a small library that also needs development extras: choose names, version policy, requirements and tool sections, and justify each from how a front-end tool reads it. - Build a failure lab: a backend name that does not exist, a
requiresentry left out, and a metadata field that contradicts the code; record the exact error each case produces and the mechanism behind it. - Take the worked example's project and make it installable from a local path on a second machine; note which of its declarations mattered during the second install and which did not.
Lesson checklist
- I can separate
[build-system](how to build) from[project](what the package is). - I can name the backend and its
requireslist and say which tool installs them. - I can say where run-time dependencies belong and where development-only ones go.
- I can point to the fields that end up as distribution metadata.
Remember
`pyproject.toml` is the one declaration file a modern project needs: `[build-system]` tells a front-end tool which backend builds the project, `[project]` states what the artifact is and what it needs, and `[tool.*]` sections carry the rest of the workflow. Once the two essential tables read as "how" and "what", the packaging pipeline is just the backend applying them.
Interview angles
What is the main purpose of **Project metadata**?+
In pyproject.toml, Project metadata names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
What should you pay attention to when using **Build-system configuration**?+
In pyproject.toml, Build-system configuration names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
How does **Dependencies** fit into this lesson?+
The dependencies list names the run-time requirements, each a requirement string such as requests>=2; front-end tools read it when installing, while packages used only for development belong in an extras or dependency-group section.
What happens if `requires-python` in `[project]` excludes the interpreter that tries to install the package?+
The installer refuses the install and reports that the package requires a different Python version, because requires-python travels in the distribution's metadata and is checked against the running interpreter.