pip install -r requirements.txt without pinned hashes is reproducible only if you also fix the network, the PyPI index, and time itself. pip-tools solves the reproducibility problem with two commands: pip-compile reads a high-level requirements.in and produces a hash-pinned requirements.txt that names every transitive dep with its SHA-256, and pip-sync makes the venv match the compiled file exactly — installing what’s missing, removing what’s extra. This article wires the workflow with operational rules for updates and CI.
How to verify
Check what’s installed against what’s pinned:
pip --version && pip list 2>/dev/null | head
pip-compile --version
diff <(pip freeze | sort) <(grep -E '^[a-z]' requirements.txt | cut -d= -f1 | sort) | head
pip check
sha256sum requirements.txt
If pip check reports a conflict, the lock has drifted from the venv — pip-sync requirements.txt to re-converge.
What’s happening
A standard requirements.txt with Django==4.2.1 pins one package. pip-tools’ compiled requirements.txt pins that package plus everything Django depends on, transitively — asgiref==3.7.2, sqlparse==0.4.4, etc. — and each entry carries a --hash=sha256:... so pip rejects any wheel whose contents don’t match. An attacker who compromises a transitive dep on PyPI cannot ship malicious code to your install because the hash won’t match what you compiled against.
The split between requirements.in (what you wrote) and requirements.txt (what the resolver produced) is the same split Poetry has between pyproject.toml and poetry.lock, but with two important differences. First, pip-tools is just pip — no manager to install, no walled garden. Second, the compiled requirements.txt is a standard pip-installable file, so any environment that runs pip can install it without learning a new tool. That makes pip-tools the safest choice for CI runners, Docker images, and one-off deployments where you don’t want to install a manager.
pip-sync is the other half. pip install adds packages but never removes them — over weeks of pip install, the venv collects dead deps that aren’t in requirements.txt. pip-sync removes anything not in the file, getting back to a known state.
The procedure
-
Install pip-tools into the project venv (not globally — version drift breaks compile output):
/opt/venvs/app/bin/pip install pip-tools /opt/venvs/app/bin/pip-compile --version -
Write
requirements.inwith high-level pins only:# requirements.in - what we want Django>=4.2,<5 psycopg[binary]>=3.1 celery[redis]>=5.3 gunicorn>=21 sentry-sdk>=1.40Don’t pin transitive deps here. The resolver figures those out and writes them to
requirements.txt. -
Compile with hashes:
pip-compile \ --generate-hashes \ --strip-extras \ --output-file requirements.txt \ requirements.in--generate-hashesis the security-critical flag — without it, you’ve pinned versions but not contents.--strip-extraskeeps the output portable to environments that don’t have the same extras configured. -
Sync the venv to match:
pip-sync requirements.txt pip checkpip-syncinstalls missing packages and uninstalls extras. After this, the venv state is exactly what’s in the file. -
For dev-only dependencies (pytest, black, mypy), keep a second pair:
requirements-dev.inandrequirements-dev.txt.requirements-dev.instarts with-c requirements.txtso it can’t pull versions incompatible with prod:-c requirements.txt pytest>=8 pytest-django black mypyCompile and sync separately. Production deploys install only
requirements.txt. -
To bump a single package without disturbing the rest:
pip-compile --upgrade-package httpx --generate-hashes requirements.inThis re-resolves only httpx (and any transitive deps that newer httpx requires), leaving everything else at its current version. Commit the diff.
-
For a full upgrade run (monthly cadence, or after a CVE alert):
pip-compile --upgrade --generate-hashes requirements.in pip-sync requirements.txt pytest -xAlways run the test suite after
--upgrade. A patch bump in a transitive dep can change behaviour.
Common pitfalls
- Editing
requirements.txtby hand to bump a version — pip-tools’ next--upgrade-packageregenerates fromrequirements.inand your edit disappears. Bump in.inonly. - Running
pip install some-packagead-hoc in the venv —pip-syncwill remove it next time. Add to.inand recompile, or accept the drift will be wiped. - Forgetting
--generate-hashesafter enabling it once — pip-tools won’t fail; it just produces an unsigned file. Add a CI check that grep’s for--hash=sha256inrequirements.txt. - Using
pip-compilefrom one Python version against an.inthat targets a different version — markers likepython_version >= "3.11"resolve to the Python you compile from. Run pip-compile inside the prod Python’s venv. - Two developers compiling on different OSes —
psycopg[binary]resolves to different wheel hashes on macOS and Linux. Standardise on Linux (a Docker container or the CI runner) for compilation.
For a managed-tool alternative that bundles compile + sync + venv management, see Poetry vs PDM vs uv. Stack Harbor’s managed operations practice runs pip-tools as the default in client repos because the output is plain pip — anyone with pip can deploy, no manager-version trap, no surprise lockfile format changes.