About requirements.txt formatter
Clean up a Python requirements.txt: normalise names, sort packages and remove duplicates.
Pin your versions
- Use == for exact versions in applications so builds are repeatable.
- Use ranges in libraries to avoid conflicts for users.
- Use a virtual environment for each project.
A worked example
A file listing Django==4.2, requests>=2.31 and a duplicate Django entry is cleaned into one alphabetical list with names normalised to lowercase. Pinned versions with == make installs repeatable, which matters in production.
Good practice
- Use a virtual environment per project.
- Generate a pinned file with pip freeze for deployment.
- Keep direct dependencies in one file and full pins in another, or use a tool such as pip-tools.
Hashes and extras
A requirement can include extras such as requests[security] and environment markers such as python_version < '3.9'. The formatter keeps these intact.
