Skip to content

Pinning Python dependencies with pip-tools and hashes

Use pip-tools to compile a hash-pinned requirements.txt from a high-level requirements.in, with operational rules for updates, security patches, and CI sync.

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

  1. 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
  2. Write requirements.in with 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.40

    Don’t pin transitive deps here. The resolver figures those out and writes them to requirements.txt.

  3. Compile with hashes:

    pip-compile \
      --generate-hashes \
      --strip-extras \
      --output-file requirements.txt \
      requirements.in

    --generate-hashes is the security-critical flag — without it, you’ve pinned versions but not contents. --strip-extras keeps the output portable to environments that don’t have the same extras configured.

  4. Sync the venv to match:

    pip-sync requirements.txt
    pip check

    pip-sync installs missing packages and uninstalls extras. After this, the venv state is exactly what’s in the file.

  5. For dev-only dependencies (pytest, black, mypy), keep a second pair: requirements-dev.in and requirements-dev.txt. requirements-dev.in starts with -c requirements.txt so it can’t pull versions incompatible with prod:

    -c requirements.txt
    pytest>=8
    pytest-django
    black
    mypy

    Compile and sync separately. Production deploys install only requirements.txt.

  6. To bump a single package without disturbing the rest:

    pip-compile --upgrade-package httpx --generate-hashes requirements.in

    This re-resolves only httpx (and any transitive deps that newer httpx requires), leaving everything else at its current version. Commit the diff.

  7. For a full upgrade run (monthly cadence, or after a CVE alert):

    pip-compile --upgrade --generate-hashes requirements.in
    pip-sync requirements.txt
    pytest -x

    Always run the test suite after --upgrade. A patch bump in a transitive dep can change behaviour.

Common pitfalls

  • Editing requirements.txt by hand to bump a version — pip-tools’ next --upgrade-package regenerates from requirements.in and your edit disappears. Bump in .in only.
  • Running pip install some-package ad-hoc in the venv — pip-sync will remove it next time. Add to .in and recompile, or accept the drift will be wiped.
  • Forgetting --generate-hashes after enabling it once — pip-tools won’t fail; it just produces an unsigned file. Add a CI check that grep’s for --hash=sha256 in requirements.txt.
  • Using pip-compile from one Python version against an .in that targets a different version — markers like python_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.