TATECHATLAS
◎ English
Programming / Tip

Describe build and project metadata in pyproject.toml without breaking repeatable pip installs

Use [build-system] for build-time dependencies and [project] for runtime dependency specifiers, then keep pyproject.toml as a declaration rather than a lockfile. For repeatable installs, generate a separate pinned requirements file, add hash-checking, or build a wheelhouse bundle instead of overloading pyproject.toml with locked install state.

On this page

Describe build-time needs in [build-system] and runtime needs in [project], then treat pyproject.toml as a declaration rather than a lockfile. For repeatable installs, generate a separate pinned requirements file, add hash-checking, or build a wheelhouse bundle from those declarations instead of trying to make pyproject.toml hold the locked install state.

Separate build-time and runtime dependencies

The pyproject.toml file is a packaging configuration file, and the specification currently defines three main tables: [build-system], [project], and [tool]. The [build-system] table declares the Python-level dependencies needed to run the build system successfully, while [project] describes the metadata and runtime dependencies of the installed package. That distinction matters for reproducibility because the build environment and the installed environment have different needs.

A common source of confusion is putting everything into one table and then expecting it to behave like a locked install file. In practice, [build-system] is about what must be present to build the distribution, and [project] is about what the package requires once it is installed. If you mix those concerns, it becomes harder to reason about which part of the file affects the build and which part affects the runtime environment.

For example, a project built with setuptools can declare a minimal build-system table with requires listing setuptools. That tells the build tool what it needs to execute the build. The runtime dependencies belong in [project] as dependency specifiers, where they describe what users and downstream environments will consume. Keeping those roles separate makes the file easier to maintain and less likely to create false expectations about repeatability.

Keep pyproject.toml as a declaration, not a lockfile

pyproject.toml can express version ranges, extras, and direct references, but it is not a lockfile. Repeatable installs usually require a separate pinned representation of the actual resolved packages. A requirements file with pinned versions, for example using the == operator, protects against newly released versions that may introduce bugs or incompatibilities. pip freeze can generate such a file, and it will pin transitive dependencies as well as top-level packages.

If you want stronger assurance, you can add hash-checking to the pinned requirements. That helps protect against a compromise of the package index or the HTTPS certificate chain, and it also guards against a package changing without its version number changing on indexes that allow that. This is useful for automated server deployments because it can reduce the need for a private index server with approved packages and access control.

If you want stronger assurance, you can add hash-checking to the pinned requirements. That helps protect against a compromise of the package index or the HTTPS certificate chain, and it also guards against a package changing without its version number changing on indexes that allow that. This is useful for automated server deployments because it can reduce the need for a private index server with approved packages and access control. pip-compile can generate a requirements file with --generate-hashes, and pip can enforce hash checking with --require-hashes. This is useful for automated server deployments because it can reduce the need for a private index server with approved packages and access control.

A practical workflow is to keep the project metadata in pyproject.toml and generate a separate requirements file for repeatable installs. If you need offline or cached installs, you can build a wheelhouse from that requirements file instead of expecting pyproject.toml to provide the bundle.

Choose a reproducibility layer that matches the deployment

There are several increasingly strict ways to make an environment more repeatable. Pinning package versions is the simplest and works across OSes and architectures, but it still trusts the locations you fetch packages from and the certificate authority chain. It also relies on those locations not allowing packages to change without a version increase, which PyPI does protect against.

Hash-checking adds another layer by verifying downloaded packages against expected hashes. This is a good fit for automated server deployments and can be a labour-saving alternative to running a private index server with approved packages. It removes some of the need to upload packages, maintain ACLs, and keep an audit trail, though it does not provide the availability benefits of a private index or a vendored library.

A wheelhouse, also called an installation bundle, goes further by packaging all dependencies with compilation already performed into a single directory that can be archived. That can help when index servers are unavailable and avoids time-consuming recompilation. The trade-off is that such a wheelhouse contains compiled packages, which are typically OS and architecture specific, so the archive is not necessarily portable across machines.

Hash-checking can also be used together with a wheelhouse, since the bundle is built from a requirements file. That can help ensure future archives are built with identical packages. The right choice depends on the deployment: pinning is easy and broad, hash-checking adds integrity protection, and wheelhouse bundles add offline and precompiled installation benefits at the cost of portability.

Avoid conflicts by not overloading pyproject.toml

Conflicts often arise when pyproject.toml is used as if it were the locked install state. The file is meant to hold packaging metadata and tool configuration, with [build-system] for build dependencies, [project] for project metadata, and [tool] for tool-specific configuration. Other tables are reserved for future use, and tool-specific configuration should use the [tool] table. That structure is a good reason to keep install-time reproducibility artifacts separate.

If you place pinned versions, hashes, or wheelhouse state directly into pyproject.toml, you can create confusion about what changes when a dependency is upgraded and what belongs to packaging versus deployment. It can also make the file harder to review, because the same file would be serving two different purposes: describing the project and recording a frozen environment.

A cleaner approach is to let pyproject.toml describe the project and the build, then maintain the repeatable install artifacts alongside it. That might mean a pinned requirements file, a hash-checked requirements file, or a wheelhouse archive generated from those files. Those artifacts can be versioned and updated independently, which keeps the packaging metadata stable while still supporting reproducible deployments.

This separation also makes it easier to choose the right reproducibility level for each environment. A development machine may only need pinning, a production server may need hash-checking, and an air-gapped or slow environment may benefit from a wheelhouse. None of those layers depends on overloading pyproject.toml, and keeping them separate reduces the chance of conflicting expectations.

Applicability

  • Does [build-system] only list the build backend and its build-time dependencies, not runtime packages?
  • Are [project] dependencies expressed as specifiers or ranges, not as a pinned install snapshot?
  • Is the repeatable install artifact (requirements file, hash-checked file, or wheelhouse) stored and versioned separately from pyproject.toml?
  • Does the chosen reproducibility layer match the deployment, including OS/architecture constraints for wheelhouse bundles?
  • Are hash-checking and wheelhouse understood as separate layers rather than substitutes for each other?

Repeatable installs still depend on the package index and the certificate chain unless additional safeguards are used. Wheelhouse bundles are typically OS and architecture specific, so they are not portable across machines by default. Hash-checking and wheelhouse are separate layers and do not replace each other. pyproject.toml can express version ranges or direct references, but it is not itself a pip lockfile, so it cannot by itself guarantee identical installed versions across environments.

Sources

  1. pip: repeatable installs ↗
  2. Python Packaging: pyproject.toml specification ↗
Back to top ↑