Skip to content

How to Pin Python Package Versions in requirements.txt

DodaTech 2 min read

In this tutorial, you'll learn about How to Pin Python Package Versions in requirements.txt. We cover key concepts, practical examples, and best practices.

The Problem

You deploy a Python app that worked yesterday, but today pip install -r requirements.txt fails or introduces breaking changes. Another developer on your team installs dependencies and gets different versions. Unpinned dependencies cause unreproducible builds and unexpected breakage. Without exact version pinning, pip install requests might give you 2.28.0 today and 3.0.0 tomorrow.

Quick Fix

1. Pin exact versions in requirements.txt

flask==3.0.3
requests==2.32.3
gunicorn==22.0.0
click==8.1.7
setuptools==70.0.0

Use == for exact pinning — never >= in production requirements. If you must allow ranges, use ~= which allows patch-level changes: flask~=3.0.0 allows 3.0.1 but not 3.1.0.

2. Generate pinned requirements from a loose specification

# Create requirements.in with loose constraints
echo "flask" > requirements.in
echo "requests" >> requirements.in
echo "gunicorn" >> requirements.in

# Use pip-compile from pip-tools
pip install pip-tools
pip-compile requirements.in  # Generates requirements.txt with pinned sub-dependencies

3. Freeze current environment

pip freeze > requirements.txt

This captures all installed packages with exact versions. Run this in a clean virtual environment for the most accurate results.

4. Pin with hashes for supply chain security

pip-compile --generate-hashes requirements.in > requirements.txt

This adds SHA256 hashes for every package. pip will verify each package against its hash before installing, protecting against package tampering.

5. Use constraints files for complex projects

# constraints.txt
flask==3.0.3
requests==2.32.3

Reference it in requirements:

pip install -c constraints.txt -r requirements.in

6. Check which packages need updating

pip list --outdated

7. Automate pinning with pip-tools

# Makefile
requirements.txt: requirements.in
	pip-compile $<

.PHONY: upgrade
upgrade:
	pip-compile --upgrade requirements.in

Common Causes

Problem Why It Happens Fix
Using >= instead of == Gets latest version on every install Pin with ==
Not including transitive deps Sub-dependencies break unexpectedly Use pip freeze or pip-compile
Different platforms Windows vs Linux packages differ Add platform markers: pywin32; sys_platform == 'win32'
No lock file No reproducible builds Generate requirements.txt from constraints.in
Forgetting to update after adding packages Missing dependencies in deployment Run pip freeze > requirements.txt after pip install

Verify Your Python Environment

python3 --version
# Python 3.10.12
pip list | grep requests
# requests            2.31.0

Check your Python version and installed packages before debugging. Many errors are caused by version mismatches between development and production environments. Using a virtual environment with pinned dependencies prevents these surprises.

Prevention

  • Always commit a requirements.txt with exact versions to version control
  • Use pip-compile to lock transitive dependencies
  • Run pip audit regularly to check pinned packages for known vulnerabilities
  • Create a clean virtual environment for each project and regenerate requirements.txt when adding dependencies

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro