Calvin Cheng
Calvin Cheng

Reputation: 36506

pip freeze and order of dependencies

`pip freeze > requirements.txt` 

automatically writes my dependencies in an apparently alphabetically order, like this:-

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

The problem with this is that pip install -r requirements.txt (when I deploy my code with its dependencies listed in requirements.txt) will end up failing because matplotlib needs numpy to be installed first.

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

Upvotes: 8

Views: 11955

Answers (4)

fabianegli
fabianegli

Reputation: 2246

A file containing the packages in the desired order can be used like so:

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 appended 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

Upvotes: 2

András Aszódi
András Aszódi

Reputation: 9660

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

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

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

And then invoke pip install twice (e.g. when installing a virtual env):

# this installs numpy
pip install -r ${NUMPY_REQS}

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

Note that this tee / grep magic combo works on Unix-like systems only. No idea how to achieve the same thing on Windows.

Upvotes: 0

Hugo Lopes Tavares
Hugo Lopes Tavares

Reputation: 30394

For your case it does not matter, because pip builds every requirements (calling python setup.py egg_info for each) and then install them all. For your specific case, it does not matter, because numpy is currently required to be installed while building matplotlib.

It is a problem with matplotlib, and they created a proposal to fix it: https://github.com/matplotlib/matplotlib/wiki/MEP11

See comments from this issue at pip issue tracker: https://github.com/pypa/pip/issues/25

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

Upvotes: 2

Vladimir Chub
Vladimir Chub

Reputation: 471

You can try command

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

This installs the packages without dependencies and possibly you will get rid above written problems.

Upvotes: 1

Related Questions