Skip to content Skip to sidebar Skip to footer

Pip Install To Custom Target Directory And Exclude Specific Dependencies

I'm looking for a method to use pip or similiar to install a list of python packages to a custom target directory (ex./mypath/python/pkgs/ ), but also exclude/blacklist specific de

Solution 1:

Faced with a similar problem, and using a running bash shell I managed to exclude specific packages with

pip install $(grep -ivE "pkg1|pkg2|pkg3" requirements.txt)

where pkg1 and so on are the names of the packages to exclude.

Solution 2:

I think this essentially can be achieved in several steps, assuming you're using virtualenv or similar...

  • If you first do a normal pip freeze > requirements.txt you'll get all of the transitive dependencies (e.g. not excluding anything.)

  • Now edit requirements.txt, remove the packages you want to exclude...

  • Finally, in a new environment do pip install -r requirements.txt -t ... --no-deps. Voila: you've installed the dependencies you wanted while excluding specific ones.

Solution 3:

An approach that takes into account sub-dependencies is to first install the exclude.txt environment, then the req.txt environment, then check the diff and finally install that into the target directory.

Example using virtualenv that will work on GNU/Linux:

virtualenv tmpenv
source tmpenv/bin/activate

pip install -r exclude.txt
pip freeze > exclude-with-deps.txt
pip install -r req.txt
pip freeze > req-with-deps.txt
comm -13 exclude-with-deps.txt req-with-deps.txt >final-req.txt

pip install -r final-req.txt --no-deps -t pypkgs

Solution 4:

You can use pip install --no-deps with pip check to exclude specific packages. A real example of mine is when I installed paddleocr on Jetson Nano, pip kept installing python-opencv for me, which has already installed by myself without using pip, but pip cannot detect it. To stop pip installing python-opencv for me, here are the steps

  1. use pip install --no-deps paddleocr to install paddleocr without its dependencies
  2. use pip check to list unresolved dependencies, reformat the output so that it can be read by pip install -r, then remove the packages you don't want to install (it is python-opencv in my case), and save it to a file named fix-deps.txt
  3. use pip install --no-deps -r fix-deps.txt to install unresolved dependencies
  4. repeat steps 2 and 3 until the output of pip check only contains the packages you don't want to install.

pip install --no-deps and pip check are very useful commands that allow you to resolve the dependencies by yourself when pip cannot do it right for you. The shortcoming of this solution is the output of pip check is designed for humans, it cannot be used by pip install -r directly, so you have to reformat the output manually or use the awk command.

PR and issue have been created for the pip community to make the output of pip check suitable for the pip install to read, but for some reason, they don't think it is a good idea. so I guess this is the best we can do now.

Refs:

  1. https://github.com/pypa/pip/pull/10108
  2. https://github.com/pypa/pip/issues/10066#issuecomment-872638361

Post a Comment for "Pip Install To Custom Target Directory And Exclude Specific Dependencies"