Sub topic 1
What pyproject.toml describes
pyproject.toml is the single configuration file at the root of a Python project. It tells build tools, installers, and developer tools what the project is, what it needs, and how to build it. This chapter explains the tables in the file and how uv reads them. Later chapters cover creating projects, dependencies, and lockfiles in detail.
Prerequisites
This chapter assumes you have:
- Basic Python programming skills, including importing modules and packages.
- Comfort running commands in a terminal.
- A general idea of what pip and virtual environments do.
You do not need uv installed to read this chapter. You do need it for the later chapters that run commands.
Why one file
Before pyproject.toml, a project's settings were spread across several files:
setup.pyheld metadata and ran arbitrary code at install time.setup.cfgheld declarative settings for setuptools.requirements.txtlisted dependencies, with no standard link to the metadata.MANIFEST.in,tox.ini, and others held tool-specific settings.
Two standards replaced this. PEP 518 introduced pyproject.toml and the [build-system] table. PEP 621 defined the [project] table for metadata. PEP 735 added [dependency-groups]. Because these are standards, any compliant tool can read them. uv, pip, and build frontends all understand [project] and [build-system].
The file uses TOML, a configuration format with [table] headers and key = value pairs. Strings use quotes, lists use square brackets, and # starts a comment.
The tables at a glance
| Table | Defined by | Purpose |
|---|---|---|
| `[project]` | PEP 621 | Name, version, Python requirement, runtime dependencies, entry points |
| `[dependency-groups]` | PEP 735 | Named sets of dependencies that are not part of the published package, such as `dev` |
| `[build-system]` | PEP 518 | Which backend builds the project |
| `[tool.*]` | Each tool | Settings for one tool, such as `[tool.uv]` or `[tool.pytest.ini_options]` |
The [project] table
This table describes the project as a distribution, which is the thing you could publish to a package index. Here is an example for a small command-line project:
[project]
name = "weather-cli"
version = "0.1.0"
description = "Print the weather from the command line"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"httpx>=0.27",
"typer>=0.12",
]
[project.scripts]
weather = "weather_cli:main"Each key has a specific job:
nameis the distribution name. Tools normalize it, soWeather_CLIandweather-clirefer to the same project. The importable package name, such asweather_cli, is separate and uses underscores.versionis the release version. It must follow the PEP 440 format, for example0.1.0or1.2.3rc1.descriptionis a one-line summary, andreadmepoints to a longer file shown on a package index.requires-pythonis a version constraint on the interpreter. Installers refuse to install the project on an interpreter outside this range. uv also uses it when choosing a Python version for the project.dependencieslists runtime requirements. Each item is a PEP 508 requirement string such ashttpx>=0.27.[project.scripts]maps a command name to a function. Installing the project creates aweatherexecutable that callsweather_cli:main. The part before the colon is a module path and the part after is a callable in it.
Version constraints
Dependency strings pair a name with a constraint. The common operators are:
| Constraint | Meaning |
|---|---|
| `httpx>=0.27` | Version 0.27 or newer |
| `httpx>=0.27,<1` | Newer than 0.27 but below 1.0 |
| `httpx~=0.27.0` | Compatible release: at least 0.27.0 and below 0.28 |
| `httpx==0.27.2` | Exactly this version |
For an application, a lower bound is usually enough, because the lockfile pins exact versions. For a library, avoid tight upper bounds. They force every downstream user to resolve against your guess about future releases.
Dynamic fields
A field can be marked as computed by the build backend instead of written in the file:
[project]
name = "example"
dynamic = ["version"]The backend then supplies the version, for example from a Git tag or a __version__ variable. If you list a field in dynamic, you must not also set it statically. Most projects start with a static version and only switch if they need automated versioning.
The [dependency-groups] table
Some dependencies are needed to work on a project but not to run it, such as test runners and linters. If you put them in [project] dependencies, every user of your published package would install them. Dependency groups solve this:
[dependency-groups]
dev = ["pytest>=8"]
lint = ["ruff"]Groups are not published in the package metadata, so users never see them. uv installs the dev group by default when you work on the project. The uv add --dev command writes to this table.
There is an older approach, [project.optional-dependencies], often called extras. Extras are published and chosen by the user at install time, for example pip install "package[postgres]". Use extras for optional features your users may want. Use dependency groups for tooling that only contributors need.
The [build-system] table
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"When someone builds your project into a wheel or source distribution, the frontend (uv, pip, or python -m build) reads this table. It installs the packages in requires into an isolated environment, then calls the named backend. The backend does the actual work of collecting files and writing metadata.
The backend is a separate choice from the frontend. Common backends include hatchling, setuptools, flit-core, and uv's own uv_build. The frontend does not care which one you pick, as long as it follows the standard interface.
If [build-system] is missing, tools fall back to legacy setuptools behavior. uv treats a project without a [build-system] table as a virtual project. In that case uv manages its dependencies but does not install the project itself into the environment.
The [tool] tables
Any tool may claim a namespace under [tool.<name>]. The standards do not define the contents. Each tool documents its own keys:
[tool.uv]
default-groups = ["dev"]
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "-q"
[tool.ruff]
line-length = 100For uv, [tool.uv] holds settings that have no standard equivalent. Examples include [tool.uv.sources], which redirects a dependency to a path, Git repository, or workspace member, and [tool.uv.workspace], which declares workspace members. Later chapters cover both.
How uv reads the file
When you run a uv project command, uv looks for pyproject.toml in the current directory and then in parent directories. It treats the first one containing a [project] table as the project root. Different commands use different parts:
uv addanduv removeedit[project] dependenciesor a dependency group.uv lockreads dependencies andrequires-python, then writesuv.lock.uv syncanduv runmake the environment match the lockfile.uv buildreads[build-system]and calls the backend.
The lockfile, uv.lock, is generated by uv and is not part of the standard. It records exact versions. pyproject.toml records what you want. The lockfile records what was chosen.
Common errors
- Invalid TOML. A missing quote or comma gives a parse error that names a line number. Strings need double or single quotes, and list items need commas.
- Setting a dynamic field statically. Listing
versionindynamicand also writingversion = "0.1.0"is invalid. - Mismatched script target.
weather = "weather_cli:main"fails at run time with an import error if the package is not namedweather_clior has nomain. The distribution nameweather-cliand the import nameweather_cliare different things. - Wrong table for tools. Putting
pytestin[project] dependenciesships it to every user. Put it in a dependency group. - Overly tight
requires-python. A constraint like==3.12.1blocks every other interpreter. Use a lower bound such as>=3.12. - Hand-editing and forgetting the lock. If you change dependencies by hand, run
uv locksouv.lockmatches. Otherwiseuv sync --lockedfails.
Exercise
You are writing a library that depends on httpx at run time and uses pytest for tests. In which table does each dependency belong, and why should pytest not go in the other one?
Show answer
httpx belongs in [project] dependencies, because users of the library need it installed to run it. pytest belongs in a dependency group such as dev under [dependency-groups]. Dependency groups are not published in the package metadata. If pytest were in [project] dependencies, every user installing the library would also install it.
Exercise
A project's pyproject.toml has no [build-system] table. What does uv do differently with it, and what would you add to make uv install the project itself into its environment?
Show answer
uv treats the project as virtual, so it installs the dependencies but not the project itself. To have it installed, add a [build-system] table with a backend, for example requires = ["hatchling"] and build-backend = "hatchling.build", and make sure the project has a package to build, such as src/<name>/__init__.py.