Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pip install to custom target directory and exclude specific dependencies

Tags:

python

pip

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 dependencies.

I want to exclude specific dependencies since they are already met from a different install path (e.g. an anaconda install). I don't have the privilege of adding packages to the default python installation (nor do I want to).

I'm currently use the -r and -t options of pip. But have not found a way to exclude specific packages.

A pip command like this is would be ideal:

pip install --log pip.log -r req.txt -t /mypath/pypkgs/ --exclude exclude.txt

--no-deps is not an option since I need some of the dependencies.

I'm currently pursuing a python script to do pip installs that include dependencies I don't need via:

pip install --log pip.log -r req.txt -t /mypath/python/pkgs/

and then (automatically) remove the unneeded dependencies after the pip install finishes.

I hoping some combination of pip commands can achieve what I'm looking for some straightforward away. I'm using pip 7.1.2. Thanks!

Similar, yet I'm not upgrading and want to specify a target path:

pip: upgrade package without upgrading particular dependency

like image 249
bscipio Avatar asked Oct 30 '15 16:10

bscipio


People also ask

How do you exclude a dependency in Python?

Pip Install Requirements – Exclude Packages Install the dependencies from the requirements. txt file using the pip command, excluding the specific packages: $ pip install -r $(grep -v '^ *#\|^pkg1\|^pkg2' requirements. txt | grep .)

Does pip install dependencies of dependencies?

Pip will not flag dependency conflicts. As a result, it will happily install multiple versions of a dependency into your project, which will likely result in errors. One way to avoid dependency conflicts is to use an alternative Python package manager, like conda, poetry or ActiveState's State Tool.

How do I set pip to specify?

To install a specific version of a Python package you can use pip: pip install YourPackage==YourVersion . For example, if you want to install an older version of Pandas you can do as follows: pip install pandas==1.1. 3 .

What is the flag in pip install?

The --user flag to pip install tells Pip to install packages in some specific directories within your home directory. This is a good way to have your own default Python environment that adds to the packages within your system directories, and therefore, does not affect the system Python installation.


3 Answers

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.

like image 154
kodemartin Avatar answered Sep 24 '22 11:09

kodemartin


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.

like image 45
thom_nic Avatar answered Sep 21 '22 11:09

thom_nic


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
like image 3
edvardlindelof Avatar answered Sep 23 '22 11:09

edvardlindelof