7

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?

3 Answers 3

9

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.

1
  • if there are transitive dependencies, to ensure pip get the right versions for all of them, you need to analyse all the setup.py files, and pip-tools can do it for you
    – JR Utily
    Aug 8, 2019 at 9:46
6

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
1
  • I was already using pip-tools for development, I didn't think of it to install things in production. But it comes in handy ! Thanks a lot.
    – JR Utily
    Aug 8, 2019 at 9:19
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.