Freeze delay and dependency order

`pip freeze > requirements.txt` 

automatically writes my dependencies in explicit alphabetical order, for example: -

 matplotlib==1.2.0 numpy==1.6.2 pandas==0.9.1 

The problem with this is that pip install -r requirements.txt (when deploying my code with its dependencies specified in requirements.txt ) will fail because matplotlib needs to be installed numpy first.

How can I guarantee that matplotlib is listed after numpy in the requirements.txt file when I pip freeze it?

+5
source share
4 answers

In your case, this does not matter, because pip builds all the requirements (by calling python setup.py egg_info for each) and then setting them all up. For your specific case, this does not matter, because numpy is currently required for installation when building matplotlib .

This is a problem with matplotlib and they created a fix proposal: https://github.com/matplotlib/matplotlib/wiki/MEP11

See the comments on this issue in the release tracking log: https://github.com/pypa/pip/issues/25

This question is a duplicate of Matplotlib requirements with installing pip in virtualenv .

0
source

You can try the command

pip install --no-deps -r requirements.txt

This installs packages without dependencies and you may get rid of the problems described above.

+1
source

Note that h5py (the Python HDF5 shell) has the same problem.

My workaround is to split the pip freeze output into two: into a short requirements file containing only the numpy version of ${NUMPY_REQS} , and a long ${REQS} containing all the other packages. Note the -v switch of the second grep , "reverse match."

 pip freeze | tee >( grep '^numpy' > ${NUMPY_REQS} ) | grep -v '^numpy' > ${REQS} 

And then call pip install twice (for example, when installing virtual env):

 # this installs numpy pip install -r ${NUMPY_REQS} # this installs everything else, h5py and/or matplotlib are happy pip install -r ${REQS} 

Please note that this magic tee / grep combination only works on Unix-like systems. I do not know how to achieve the same in Windows.

0
source

The file containing the packages in the desired order can be used like this:

 pip freeze -r sorted-package-list.txt > requirements.txt 

Where sorted-package-list.txt contains

 numpy matplotlib 

Note. Packages not included in the sorted-package-list.txt file are added at the end of the requirements file.

Example result:

 numpy==1.14.1 matplotlib==2.2.3 ## The following requirements were added by pip freeze: pandas==0.23.4 
0
source

All Articles