Say for example I add django to my venv with pip install django
. The usual way to generate my requirements.txt file is with pip freeze > requirements.txt
which produces:
asgiref==3.2.3
Django==3.0.2
pytz==2019.3
sqlparse==0.3.0
This is a lie. My only requirement is django and the (perfectly effective) requirements.txt I want is:
Django>=3.0.2, <4
There are times when this matters. With a real application I will end up with many packages that have overlapping dependencies. When upgrading this may or may not cause a problem but there will almost always be problems if I've pinned all those dependent packages I don't care about (in the example above, they are up to the django developers, not me).
In order to optimize a dependency bloated requirements.txt file I can manually inspect the source code (resulting in an empty requirements.txt above ;)) but for any real application this quickly becomes impractical.