pip: install dependencies of dependencies

2020-07-18 10:19发布

问题:

I would like to manually install all the requirements of a Python package I'm writing. To this end, I created the file requirements.txt and added the dependencies, line by line:

$ cat requirements.txt
meshio
numpy

When running

$ pip install -r requirements.txt

those two packages are installed alright, but I noticed that the dependencies of meshio aren't (i.e., whatever is listed in its requirements.txt). Not surprising, how is pip supposed to know?

Is there a mechanism for installing the entire dependency tree with pip?

回答1:

You may be interested by pip-tools, a python package that can be used to build a requirements.txt file that takes into account all underlying dependencies. It can be installed via pip:

pip install --upgrade pip  # pip-tools needs pip>=6.
pip install pip-tools

Once installed, you can use the pip-compile command to generate your requirements file. For example, suppose you work on a Flask project. You would have to do the following:

Write the following line to a file:

Flask

Run pip-compile <your-file>. It will produce your requirements.txt, with all the dependencies pinned. You can re-run pip-compile to update the packages. Your output file will look like this:

#
# This file is autogenerated by pip-compile
# Make changes in requirements.in, then run this to update:
#
#    pip-compile <your-file>
#
flask==0.10.1
itsdangerous==0.24        # via flask
jinja2==2.7.3             # via flask
markupsafe==0.23          # via jinja2
werkzeug==0.10.4          # via flask


回答2:

Turns out for the dependencies to be installed, the packages needs to list its dependencies as

install_requires=[
    'numpy',
    'pyyaml'
    ],

as part of setup() in setup.py, not in requirements.txt.



回答3:

I'm not sure if this is what you need. But this is how I solve this problem. I create a virtual environment for each of my python projects.

cd my_project_folder
virtualenv venv # choose the python interpreter you need ;)
source venv/bin/activate

Then I install all the needed packages for the project.

pip install <what ever packages you need>

If you then need to create a requirements.txt you can use this command

pip freeze > requirements.txt  # including the installed version of each package

To leave a virtual environment just type deactivate. Hope this helps you...

This is also in detail described here