Separating Build Dependencies from Runtime Dependencies in pyproject.toml
To avoid version conflicts and ensure reproducible installs, use [build-system].requires for build tools only and [project].dependencies for runtime libraries. Pin exact versions in a separate requirements.txt generated with pip freeze or pip-compile.
On this page
Key idea
The pyproject.toml format provides two independent dependency contexts. The [build-system] table describes the tools required to run the project's build system, such as setuptools or hatchling. These dependencies are installed into an isolated temporary build environment. This isolation prevents version conflicts with the libraries your code uses at runtime, which are declared in the [project] table under the dependencies key. To make installs reproducible, you must pin exact versions of all dependencies. pyproject.toml dependency specifiers allow version ranges, so they do not guarantee identical results when new package versions appear on the index. Generate a requirements file with pip freeze, which records the exact versions of every installed distribution, including transitive dependencies. Alternatively, use pip-compile from the pip-tools package to compile a locked requirements file from your input requirements. Pinning alone protects against newly released versions introducing bugs or incompatibilities. For stronger guarantees, add artifact hashes. pip-compile --generate-hashes emits a requirements file whose lines include sha256 hashes alongside pinned versions. Installing with pip --require-hashes makes pip verify every downloaded artifact against those hashes, protecting against a compromised package index or HTTPS certificate chain. In contrast, pip freeze records only names and versions, not hashes. When installing from a pinned requirements file, use --no-deps so pip does not attempt to resolve dependencies again and silently upgrade past the listed versions. A wheelhouse built with pip wheel provides installation when index servers are unavailable, though compiled wheels are typically OS- and architecture-specific.
Context: two independent dependency lists in pyproject.toml
The pyproject.toml file defines two independent sets of dependencies that are resolved at different stages of a package's lifecycle.
The [build-system] table with its requires key lists only the tools the build backend needs, such as setuptools or poetry-core. These are installed into an isolated temporary build environment.
The [project] table with its dependencies key lists the libraries your code imports at runtime. They are installed into the user's or server's environment together with your package.
[build-system]
requires = ["setuptools"]
[project]
dependencies = ["requests>=2.28"]Why mixing the two breaks reproducibility
Putting runtime libraries in [build-system].requires mixes concerns: build tools run in an isolated environment, so any runtime library placed there is unnecessary and may conflict with the user's own versions.
Declarative dependency ranges in pyproject.toml do not by themselves guarantee reproducibility, because new releases can appear in the repository whenever you build.
A separate requirements file with exact version pins (==) fixes the versions of top-level packages and their transitive dependencies, while hash-checking mode additionally verifies artifact integrity.
Worked example: pinning and hash-checking a setuptools project
The minimal project example above separates concerns: only build tooling appears under [build-system], and only user-facing libraries under [project].
After installing the environment, pip freeze writes a requirements.txt with exact versions of every installed distribution, including transitive dependencies.
pip-compile from pip-tools goes further: --generate-hashes adds a sha256 hash to each pinned line, and installing with --require-hashes makes pip verify every download against those hashes.
pip freeze > requirements.txt
python -m pip install --no-deps -r requirements.txt
# Alternatively, compile a locked file with hashes:
pip-compile --generate-hashes pyproject.toml -o requirements.txt
python -m pip install --require-hashes --no-deps -r requirements.txtBoundaries of applicability
Version pinning trusts the locations you fetch packages from (such as PyPI) and the certificate authority chain; it does not protect against repository compromise.
Hash-checking mode protects integrity but does not provide the availability benefits of a private index or a vendored library; it is a labor-saving alternative to running a private index with ACLs and audit trails.
A wheelhouse contains compiled packages that are typically OS- and architecture-specific, so archives are not necessarily portable across machines. Combining a wheelhouse with a hash-checked requirements file lets future archives be rebuilt from identical packages.
Applicability
- Does [build-system].requires contain only build tooling such as setuptools or hatchling?
- Are exact versions pinned in requirements.txt via pip freeze or pip-compile?
- Do you install with --no-deps to prevent re-resolution?
- If you need integrity guarantees, have you generated hashes with pip-compile --generate-hashes?
Where this applies
Version pinning in requirements.txt trusts PyPI and the certificate authority chain and does not guard against repository compromise. Hash-checking mode protects integrity but offers no availability benefit over a private index or vendored library. A wheelhouse is not portable across operating systems and architectures. pyproject.toml remains a declaration and is not itself a lock file.