How do I manage dependencies with requirements.txt and modern alternatives?
· Category: Python Programming
Short answer
requirements.txt lists packages and versions for pip install -r. Modern tools like pip-tools, poetry, and uv add lock files, dependency resolution, and virtual environment management for more reproducible builds.
Steps
- Write direct dependencies in
requirements.txtorpyproject.toml. - Generate a lock file with your chosen tool.
- Install from the lock file in CI and production.
# requirements.txt
requests>=2.31.0
pandas==2.0.3
pip install -r requirements.txt
Tips
- Pin versions for reproducibility, but leave room for patches with
>=or~=. - Separate
requirements-dev.txtfor tools likepytest,black, andmypy. poetryanduvmanage environments and lock files automatically.
# pyproject.toml (Poetry example)
[tool.poetry.dependencies]
python = "^3.11"
requests = "^2.31"
Common issues
- Unpinned dependencies can lead to broken builds when upstream packages release incompatible versions.
- Conflicting sub-dependencies are hard to resolve manually; use a lock file.
pip freezecaptures all installed packages, including transitive ones, which can obscure direct dependencies.