TATECHATLAS
◎ English
Programming / Tip

Python releases: separate dependency pins, artifact hashes, and build inputs

Exact version pins help repeat installs, while hashes check downloaded files. Neither guarantees identical build output. Record the build backend, Python version, platform, and relevant environment inputs.

On this page

Keep runtime dependencies and build requirements separate. Use version pins for repeatable installs. Generate artifact hashes explicitly and enable pip hash-checking during installation. These controls verify package inputs, but they do not prove that rebuilding the project produces byte-identical distributions.

Separate runtime dependencies from build-system requirements in pyproject.toml

PEP 621 places runtime dependencies in the dependencies field of the [project] table. The requires field in [build-system] lists Python packages needed by the build backend. The [build-system] table is optional in pyproject.toml; build frontends use the specification's default behavior when it is absent. Declare the table when you need to record a specific backend and its requirements. This makes build inputs visible, but a written list alone does not guarantee a repeatable or byte-identical build.

Why pinning and hash-checking address different trust boundaries

Pinning package versions with == in a requirements file protects against upstream version changes that could introduce bugs or incompatibilities. However, it still trusts the package index (like PyPI) and the TLS certificate chain. An attacker who compromises the index or performs a man-in-the-middle attack could serve a malicious package with the same version number.

Hash-checking mode, enabled with --require-hashes and --hash=sha256:... entries, verifies the downloaded artifact byte-for-byte against a known digest. This guards against index compromise, certificate chain attacks, and silent re-uploads of a package without a version bump (on indexes that allow it). The pip documentation states that hash-checking is a labour-saving alternative to running a private index server: it removes the need to upload packages, maintain ACLs, and keep an audit trail, while a VCS provides that trail for the requirements file. The trade-off is maintenance cost: every time a dependency version changes, the hashes must be regenerated and reviewed. This approach is well-suited for automated server deployments where the environment is controlled.

Concrete example: a release contract with both layers

A small requirements.in file lists the project's dependencies. Generate a pinned file with pip-compile --generate-hashes --output-file requirements-pinned.txt requirements.in. The generated file records versions and SHA-256 hashes for the resolved distributions. By contrast, pip freeze reports installed distributions and versions, but does not create artifact hashes. Install with pip install --require-hashes -r requirements-pinned.txt to require downloaded files to match a listed digest. Hash checking verifies package artifacts at install time; it does not show how a wheel was built or guarantee identical output from rebuilding source. Do not use shortened sample hashes. Generate complete entries, review the resolved packages, and keep the resulting requirements file with the project.

python -m pip install pip-tools
pip-compile --generate-hashes --output-file requirements-pinned.txt requirements.in
python -m pip install --require-hashes -r requirements-pinned.txt

Limits of this approach

Wheelhouse archives containing compiled wheels are usually tied to an operating system and architecture, so separate bundles may be needed for different targets. Hash checking does not provide package availability during an index outage, and it does not validate code after a trusted package is installed. Omitting [build-system] relies on specification defaults; declare the table when the backend and build requirements need to be explicit. A lock file also depends on the initial dependency resolution, so review package sources and changes. Updating a dependency requires regenerating and reviewing its hashes. Byte-identical rebuilds need additional controls over the build environment and evidence from comparing the produced artifacts.

Applicability

  • Are runtime dependencies and build backend requirements recorded separately?
  • Do you need exact version pins, hash checks for downloaded packages, or evidence of repeatable build output?
  • Have you declared a build backend or documented the default semantics used when [build-system] is absent?
  • Are hashes generated with pip-compile --generate-hashes and enforced with pip install --require-hashes?
  • Will you regenerate and review hashes when a dependency changes?
  • Do your target platforms differ in Python version, operating system, architecture, compiler, or build settings?

Hash checks compare downloaded files with approved digests, assuming the requirements file is trusted. They do not make packages available during an index outage, validate the package code, or ensure identical output from a source build. Build results can vary with Python, operating system, architecture, compiler, build backend, and environment settings. Record and control inputs that matter, then compare build outputs to demonstrate reproducibility.

Sources

  1. pip: repeatable installs ↗
  2. Python Packaging: pyproject.toml specification ↗
  3. pip-tools documentation: generate hashes ↗
Back to top ↑