PIP: Installing only the dependencies

Question:

I have a script that creates a virtualenv, installs distribute and pip in it and then optionally clones a git repo.

Now I have the project I will be working on, installed. But its dependencies are not installed. How can I make pip install all the dependencies as if I have issued a pip install MyApp?

EDIT: Appareantly my question is a duplicate of this one.

Not exactly sure but pip install -e . seems to do what I want without too many extra stuff lying around. I’d prefer if my code wasn’t linked from site-packages though.

Asked By: muhuk

||

Answers:

You should use the pip requirements file.

Essentially, place all your requirements, one in each line in a file and pass that to pip using the command

pip install -r requirements.txt

What more, if you have a standard environment, pip can actually dump such a file from existing installs using the command:

pip freeze

You can put the file thus generated directly into the pip requirements, and call the previous command from your deployment script.

Pretty cool, isnt it? 🙂

Answered By: lprsd

In my package root issuing pip install -e . installs dependencies.

Answered By: muhuk

If your dependencies are defined in the setup.py file, you can first dump them to an external file using:

python setup.py egg_info

This will list all your dependencies in YOUR_PROJECT.egg-info/requires.txt file. Then you can install them using pip:

pip install -r *.egg-info/requires.txt

to delete what you just created:

rm -rf *.egg-info/

to save some time copy pasting:

python setup.py egg_info
pip install -r *.egg-info/requires.txt
rm -rf *.egg-info/
Answered By: Jakub Kukul

To install your project’s dependencies (i.e. install_requires + extra_requires) you have to extract your dependencies using setuptools egg-info and then install the filtered list of the combined dependencies:

python setup.py egg_info
pip install `grep -v '^[' *.egg-info/requires.txt`
Answered By: jfaleiro

You can use pip-tools to create a requirements.txt that only contains the dependencies of your package:

$ pip-compile -o requirements.txt setup.py

Note that the command above only works if you do not already have a requirements.txt file. If you happen to have one already, just delete it.

Using the generated requirements.txt you can then run pip to install the dependencies:

$ pip install -r requirements.txt

Bonus 1:

The requirements.txt will include comments that indicate where the regarding dependency originates from.

Bonus 2:

If you have have an extras_require section for optional dependencies in your setup.py that looks e.g. like this:

    ...
    extras_require={
        "development": [
            "wheel",
            "debugpy",
            "pytest",
        ],
    },
    ...

You can create the requirements.txt including the optional dependencies by using:

$ pip-compile -o requirements.txt --extra development setup.py
Answered By: Salo
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.