Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I generate requirements.txt? Pip Freeze not a good way

Tags:

python-3.x

pip

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.

like image 748
Tommy Avatar asked May 21 '15 03:05

Tommy


People also ask

Why you shouldn't use pip freeze?

pip freeze might seem very useful initially but it can mess up your project because of the following reasons: It dumps all the libraries installed in your project including dependencies and sub-dependencies in the requirements. txt file. It still misses out on the libraries that are not installed using pip.

How do I freeze a requirement txt in pip?

With your chosen environment selected using the Python: Select Interpreter command, run the Terminal: Create New Terminal command (Ctrl+Shift+`)) to open a terminal with that environment activated. In the terminal, run pip freeze > requirements. txt to create the requirements. txt file in your project folder.

When should I freeze pip?

Therefore, you should use pip list and pip freeze as follows: If you want to check a list of packages with various conditions, use pip list . If you want to create requirements. txt , use pip freeze .


2 Answers

  1. Install the pipreqs library (e.g. conda install -c conda-forge pipreqs)
  2. Change dir to the project folder (cd your/repository)
  3. Run the command pipreqs --force

Or just pipreqs --force your/repository.

See additional information in the official source: https://pypi.org/project/pipreqs/

like image 119
Aleksandr Mirlenko Avatar answered Sep 24 '22 15:09

Aleksandr Mirlenko


There is a python module called pipreqs . It generates requirements.txt based on imports in the project.

like image 28
gopiariv Avatar answered Sep 24 '22 15:09

gopiariv