Skip to content

Managing Python dependencies with pip and requirements files

Use requirements.txt, constraints files, and pip-tools to get reproducible Python installs across CI and production hosts.

A requirements.txt with Flask==2.3.* looks pinned but is not — the next CI run can pull a different patch and a different transitive set, and that is how production stops matching what was tested. This article lays out a dependency workflow that actually reproduces: top-level deps in one file, a lockfile generated from them, and pip install with the flags that prevent surprises.

How to verify

Check how dependencies are currently expressed and pinned:

ls /var/www/app/requirements*.txt /var/www/app/pyproject.toml 2>/dev/null
head /var/www/app/requirements.txt
/opt/venvs/app/bin/pip list --format=freeze | wc -l
/opt/venvs/app/bin/pip list --outdated | head
/opt/venvs/app/bin/pip check

pip check is the underused command — it reports incompatible versions across the installed set. If it returns “No broken requirements found”, the venv is internally consistent; if not, the pinning is lying somewhere.

What’s happening

pip install package resolves a version graph at install time. Without a lockfile, two installs separated by an hour can resolve to different versions because PyPI got an update in between. requirements.txt with == pins is closer but still incomplete — it pins direct deps without pinning transitive ones, so requests==2.31.0 is fixed but the urllib3 it pulls is whatever current.

The fix is two files. requirements.in holds the human-curated top-level deps with loose constraints. requirements.txt is the machine-generated lockfile that lists every package — direct and transitive — at exact versions with hashes. pip-compile (from pip-tools) or uv pip compile produces the lockfile from the input.

A separate constraints.txt covers a different case: “if any of these packages happen to get installed transitively, do not exceed this version”. Constraints are not requirements; they restrict, they do not install.

The procedure

  1. Install pip-tools into the project venv (or use uv which is faster but has the same model):

    /opt/venvs/app/bin/pip install pip-tools
  2. Write requirements.in with the apps direct deps and loose ranges:

    # requirements.in
    Flask>=2.3,<3.0
    gunicorn>=21,<23
    psycopg2-binary>=2.9
    redis>=5.0
    sentry-sdk[flask]>=2.0
  3. Compile the lockfile with hashes:

    cd /var/www/app
    /opt/venvs/app/bin/pip-compile --generate-hashes --output-file requirements.txt requirements.in

    The output requirements.txt lists every package (direct + transitive) at exact versions with SHA256 hashes. Commit both files.

  4. Install from the lockfile on production:

    /opt/venvs/app/bin/pip install --require-hashes --no-deps -r requirements.txt

    --require-hashes makes pip refuse to install anything not in the lockfile with a matching hash. --no-deps prevents pip from re-resolving — the lockfile already lists every dependency.

  5. To upgrade a dep, edit requirements.in (or run pip-compile --upgrade-package), regenerate the lock, and commit the diff:

    /opt/venvs/app/bin/pip-compile --upgrade-package Flask --generate-hashes --output-file requirements.txt requirements.in
    git diff requirements.txt

Operational notes

  • pip install -r requirements.txt (no --require-hashes) is not reproducible. Hashes are the difference between “we have a lockfile” and “we have a wishlist”.
  • psycopg2-binary ships pre-built but psycopg2 (no -binary) compiles against libpq at install time — different file, different transitive linkage. Be intentional which one is in the lockfile.
  • A dev-requirements.txt separate from requirements.txt keeps test deps (pytest, coverage, black) out of production images. Compile each from its own .in with requirements.txt as a constraint to keep versions aligned.
  • pip’s resolver is much better than it was in 2020, but it still struggles with very large dep graphs. uv pip compile is significantly faster (Rust implementation) and a drop-in replacement.
  • After a security advisory, pip-audit or pip list --outdated --format=json | jq lets you script the upgrade — do not click through GitHub Dependabot PRs one by one if you have 40 services.

For the build pipeline, the same lockfile gets baked into the Docker image or copied to the host via configuration management. Stack Harbor’s managed operations practice keeps every Python service on a hash-locked install — and when a CVE lands, the upgrade path is a one-line pip-compile --upgrade-package, not an emergency triage of which packages are even in the venv.