Reproducible Python Dependency Installation: Version Pins, SHA-256 Checks, and a Wheelhouse
Reproducibility is built in layers: exact version pins, SHA-256 artifact hashes, and a bundled wheelhouse for offline installs. Each layer removes a different source of drift, and none of them replaces pyproject.toml, which describes how the project itself is built.
On this page
The short answer
Create a clean project environment, record exact versions of every distribution with pip freeze into requirements.txt (this step only pins versions, no hashes). Then generate SHA-256 hashes for those distributions (e.g., with pip-compile --generate-hashes or pip hash) and add them to requirements.txt so each downloaded artifact is verified byte-for-byte. Build a wheelhouse with pip wheel from that same file so installation can run offline without recompilation. Keep pyproject.toml as the build contract for the project itself, while requirements.txt remains the deployment contract for exact runtime versions and artifact integrity. Pinning alone trusts the index and its certificate chain; hashes protect artifact identity; the wheelhouse protects availability. Wheel files are OS and architecture specific, so rebuild bundles per target platform. For automation, generate files with pip-tools (pip-compile emits hashes only with --generate-hashes) and enforce them during installation.
Pinning Versions with pip freeze for Stable Installs
Reproducibility starts by recording which distribution versions a known-good environment actually contains. pip freeze writes one name==version line per installed distribution, covering transitive dependencies as well as the packages you named directly, so a later install resolves the same versions instead of silently selecting newer releases. The output describes installed distributions and their versions only; it contains no hashes, and it includes everything present in the environment, including packages you may not need.
Generate the file in a fresh virtual environment that received only the project's intended runtime dependencies. The operation below runs on Python 3.6 or later; pip 20.3 or later is recommended for current dependency resolution. Adding --no-deps at install time is extra insurance: pip installs only what is explicitly listed, so an omission fails visibly rather than being filled in from the index.
The example output shown after the redirect is illustrative of pip freeze formatting, not an observed run in this article.
python -m venv .venv # Python 3.6+, pip 20.3+ recommended for current resolution behaviorsource .venv/bin/activatepython -m pip install --upgrade pippython -m pip install requests==2.28.1python -m pip freeze > requirements.txtpython -m pip install --no-deps -r requirements.txt# Illustrative requirements.txt contents:requests==2.28.1charset-normalizer==2.1.0idna==3.3urllib3==1.26.9SHA-256 Hash Checking Against Artifact Tampering
Version pins constrain which release pip may fetch, but they do not bind the fetched bytes. Adding --hash=sha256:<digest> makes pip verify the downloaded artifact against a recorded digest, which protects against a compromised index or certificate chain, and against a package whose contents changed without a version bump on an index that permits such changes.
Hash-checking mode is well suited to automated deployments: it avoids running a private index with uploads, access control lists, and an audit trail, since version control history of the requirements file already records who changed what. The digests must be produced from artifacts of the target platform, because wheels for different operating systems or architectures differ and therefore have different hashes.
Hash checking does not provide the availability guarantee of a private index or vendored copy; it verifies identity only.
python -m pip download -r requirements.txt -d ./distpython -m pip hash ./dist/requests-2.28.1-py3-none-any.whl# Illustrative line added to requirements.txt --hash=sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824Creating a Wheelhouse for Offline Installation
A wheelhouse is a directory holding prebuilt wheel files for every dependency, usually archived as one tarball. Building it on a connected machine performs any compilation once, so an offline target machine installs prepared wheels instead of rebuilding native code or contacting an index. Installation from the bundle forces local files with --no-index and, in strict mode, --no-deps.
The command below requires network access during bundle creation and assumes requirements.txt exists in the current directory. The tarball is a plain archive that can be copied into an air-gapped environment.
python -m pip wheel -r requirements.txt --wheel-dir=./wheelhouse -o wheelhouse.tar.gz -C ./wheelhouse .Configuring pyproject.toml for Reproducible Builds
pyproject.toml is the TOML configuration file for packaging tools. The [build-system] table declares dependencies required to run the chosen build backend, with requires holding a list of version specifier strings; the [project] table carries project metadata such as name, version, and requires-python. Declaring requires-python records the interpreter range, which is a prerequisite for reproducing the build rather than a guarantee that every machine behaves identically.
This file describes how the project itself is built. It does not pin the resolved runtime dependency graph, so it does not substitute for a locked requirements file at deployment time.
[build-system]requires = [setuptools>=61.0.0][project]name = my_projectversion = 0.1.0requires-python = >=3.8Generating Lock Files with pip-tools
pip-tools extends pip with a two-file workflow: you list direct dependencies in a loose input file, and pip-compile resolves them plus transitives into a pinned output file. Hashes are not emitted by default, so pass --generate-hashes when you want digest lines included; the resulting file then triggers pip's hash-checking behavior during install, or you can enforce presence explicitly with --require-hashes.
pip-sync then makes an environment match the compiled file exactly, removing distributions that are not listed. pip-compile and pip-sync require the pip-tools package; install it with python -m pip install pip-tools.
python -m pip install pip-toolsecho requests~=2.28 > requirements.inpip-compile --generate-hashes --output-file=requirements.txt requirements.inpip-sync requirements.txtVerifying Artifact Integrity Before Deployment
In deployment pipelines, treat the requirements file as the integrity manifest. When any requirement carries a hash, pip verifies each downloaded artifact; using --require-hashes additionally demands a hash for every requirement, so a partially hashed file fails instead of quietly skipping verification. This is a structural check performed by pip: it confirms that bytes match recorded digests.
It is a separate matter whether the digests correspond to versions your policy approves. That review belongs to code review of the requirements file, not to pip's download step, and it does not measure bug rates or security outcomes.
python -m pip install --require-hashes -r requirements.txtLimits and Trade-offs of a Wheelhouse
Wheel files carry platform tags in their filenames. A bundle built on Linux x86_64 generally contains wheels that cannot be installed on Windows or on another CPU architecture, and some distributions publish no wheel at all, forcing a source build on the target. Rebuild the bundle per target platform, Python version, and interpreter ABI, and record the environment used to build it.
Hash-checking does not help if a dependency changes without a version change on an index that allows it and the hash file was updated at the same time; review remains a human or policy step. A wheelhouse also gives no availability guarantee if a wheel is missing, whereas a private index can serve approved artifacts on demand. The comparison is between offline completeness and index maintenance cost, not between secure and insecure options.
# Wheel filenames encode portability constraintscp38-cp38-manylinux_2_17_x86_64.whl # Linux x86_64, CPython 3.8cp311-cp311-win_amd64.whl # Windows 64-bit, CPython 3.11Combining pyproject.toml and requirements.txt
Use the two files for different jobs. pyproject.toml declares the build backend, its requirements, project metadata, and the supported Python range, so building the project follows the same declared contract on every machine. requirements.txt, optionally generated by pip-tools with hashes, records the exact runtime versions and artifact digests for installation.
A practical sequence is: declare and build with pyproject.toml, resolve and hash the runtime environment into requirements.txt, produce a wheelhouse from that file for offline targets, and verify installs with hash enforcement. Neither file supersedes the other, and neither alone detects an environment that was modified after installation.
python -m pip wheel . --wheel-dir=./distpython -m pip wheel -r requirements.txt --wheel-dir=./wheelhousepython -m pip install --require-hashes -r requirements.txtThings to check
- requirements.txt contains name==version for direct and transitive dependencies; pip freeze output contains no hash lines
- pip install runs with --no-deps or --require-hashes in deployment
- every hash line uses the sha256: prefix and matches a downloaded artifact
- wheelhouse is rebuilt for each target OS, CPU architecture, and CPython ABI
- offline install uses --no-index and only files inside the wheelhouse
- pyproject.toml declares [build-system].requires and requires-python
- hash generation (pip-compile --generate-hashes) is separate from hash enforcement
- pip freeze output was produced in a clean project-only environment
- pip version is not documented in the article
Where this applies
Version pins fix names and versions, not file contents; hashes verify downloaded bytes but do not prove that a version is bug-free or policy-approved, and a wheelhouse only helps when it contains a wheel for the exact OS, architecture, Python version, and ABI of the target. pip freeze also records packages that may be unused, pyproject.toml does not replace requirements.txt for runtime installation, and neither mechanism guarantees that an environment stays unchanged after installation.