Reputation: 13652
How should I generate requirements.txt for Python projects?
Here is the problem I am having with pip freeze. Suppose my package P requires A, B, C. Suppose C is a library that imports X, Y, Z, but only X is needed by P. Then if I:
1) Install A
2) Install B
3) Install C, which installs X, Y, Z
4) Do a pip freeze into P's requirements.txt
Then P's requirements.txt will look like:
1) A
2) B
3) C
4) X
5) Y
6) Z
But Y and Z are not actually required in my Python installation for P to run.
Many of the answers assume that, Y must be. However, python is a dynamic language. It is very often the case that, for example C is a huge library that uses numpy
or pandas
for some functionality; but P doesn't call that part of the library - in this case, I don't really need to pull those in if I know what parts of C that P needs. If all libraries were "small"; this would be rare, however there are a lot of "kitchen sink" libraries.
As far as I can tell, running pip freeze
to generate P's requirements will show you all dependencies of dependencies, and thus is a superset of P's actual dependencies.
Upvotes: 11
Views: 18063
Reputation: 1482
I've answered this question in a different stackoverflow post https://stackoverflow.com/a/65666949/1512555 where I recommended using pip-compile
from pip-tools
Upvotes: 1
Reputation: 491
pipreqs
library (e.g. conda install -c conda-forge pipreqs
)cd your/repository
)pipreqs --force
Or just pipreqs --force your/repository
.
See additional information in the official source: https://pypi.org/project/pipreqs/
Upvotes: 3
Reputation: 494
There is a python module called pipreqs . It generates requirements.txt based on imports in the project.
Upvotes: 6
Reputation: 30151
The purpose of a virtualenv is to have total control over the packages installed.
Suppose you only listed A, B, C, and X. Every time you create a new virtualenv from that requirements file, you'll get the latest versions of Y and Z. There are several problems with this:
pip freeze
is not designed to figure out minimal requirements. It is designed to enable deploying a complete application to many different environments consistently. That means it will err on the side of caution and list everything which could reasonably affect your project.For these reasons, you should not try to remove Y and Z from your requirements file.
Upvotes: 13