Pip install requirements.txt with -t for some libraries [duplicate] - python

When I have, for example, a requirements-dev.txt and a requirements.txt, I know I can have -r requirements.txt inside requirements-dev.txt, for example, and running pip install -r requirements-dev.txt would install packages from both files.
That said, I was certain that any install option would work fine inside a requirements file. Turns out that when I place inside a requirements file something like:
mypackage==1.0.0 -t /path/to/local/dir
I get:
pip: error: no such option: -t
while running pip install mypackage==1.0.0 -t /path/to/local/dir works just fine. For complicated reasons, I need to place multiple packages in one requirements file, where some packages must target one directory, others must target another, and so goes on.
Any solutions to make this work?

As of today (in pip version 21.2.4), the -t, --target <dir> option is not supported in requirements.txt files. The section "Requirements File Format" of pip's User guide lists the currently supported options:
-i, --index-url
--extra-index-url
--no-index
-c, --constraint
-r, --requirement
-e, --editable
-f, --find-links
--no-binary
--only-binary
--prefer-binary
--require-hashes
--pre
--trusted-host
--use-feature

pip install -r requirements.txt -t /path/to/install
This should work. It worked for me.
If you want different modules to be installed to different locations, then I think you might have to put them into multiple requirements text files. This is at least as far as I know

Related

Pip install requirements in offline mode linux

I am working in an offline Linux env. (RedHat 7.6)
until today I've used the full path to install
the files with pip, and it works great. (still, do)
Now on automated testing, I want to create a virtual
environment and pip install a requirements file.
The problem is, it keeps searching the web,
even though I've used --prefix, and tried --target
I can't get it to install from a certain folder,
always try to search the web
requirements file:
numpy==1.16.4
folder:
/custom_dev/install/
inside the folder:
numpy-1.16.4-cp37-37m-manylinux_x86_64.whl
tried:
pip3 install -r requirements.txt --target=/custom_dev/install/
pip3 install -r requirements.txt --prefix=/custom_dev/install/
and other stuff from StackOverflow, I've yet to find a solution to my problem, or a thread with the same one, suggestions?
ty!
Our pip-local does that:
c:\srv\bin> cat pip-local.bat
#echo off
rem pip install with `--upgrade --no-deps --no-index --find-links=file:///%SRV%/wheelhouse`
pip %* --upgrade --no-deps --no-index --find-links=file:///%SRV%/wheelhouse
the linux version uses $* instead of %* and $SRV instead of %SRV%:
pip $* --upgrade --no-deps --no-index --find-links=file:///${SRV}/wheelhouse
You can remove the --no-deps if you want dependencies to be found as well (although it will search the web if it can't find a wheel satisfying a dependency in your wheelhouse).
The companion tool is getwheel
c:\srv\bin> cat getwheel.bat
#echo off
rem
rem Download wheel file for package (getwheel foo==1.4.1)
rem
pip wheel --wheel-dir=%SRV%\wheelhouse %*
linux version:
pip wheel --wheel-dir=${SRV}/wheelhouse $*
which is used like:
getwheel numpy==1.16.4
or
getwheel -r requirements.txt
which causes the wheels of the package and its dependencies to be placed in the wheelhouse folder.
pip3 install -r requirements.txt --find-links=/custom_dev/install/ --no-index
The keyword to prevent pip to connect to PyPI via the network is --no-index.

Issue using pip download -r requirements.txt

I have my Python script and my requirements.txt ready.
What I want to do is to get all the packages listed in the "requirements.txt" into a folder. In the bundle, I'd for example have the full packages of "pymysql", "bs4" as well as all their dependencies.
I have absolutely no idea how to do this. Could you help me please? I am stuck and I am really struggling with this.
I am using Python 3.6
I am using "pip download -r requirements.txt" but it's not downloading the dependencies and outputs me only.whl files whereas I'm looking for "proper" folders..
To make pip prefer source files over wheels, use the --no-binary flag:
pip download -r requirements.txt --no-binary :all: -d /path/to/download/dir

What does " -r " do in pip install -r requirements.txt

I looked up how to install multiple packages from a requirements document using pip. The answers were mostly:
pip install -r requirements.txt
What does the -r do though? I can't find an answer for this and it isn't listed when I run pip help.
Instead of pip --help, look into pip install --help:
-r, --requirement Install from the given requirements
file. This option can be used multiple
times.
Also see these documentation paragraphs:
pip install
Requirements Files.
-r will search for requirement file.
pip install --help
will help you !!
May, 2022 Update:
If you run this command below without "-r":
pip install requirements.txt
You will get this error below:
ERROR: Could not find a version that satisfies the requirement requirements.txt (from versions: none)
HINT: You are attempting to install a package literally named "requirements.txt" (which cannot exist). Consider using the '-r' flag to install the packages listed in requirements.txt
ERROR: No matching distribution found for requirements.txt
Because "pip" tries to install the package "requirements.txt" instead of installing the packages listed in "requirements.txt". Of cource, the package "requirements.txt" doesn't exist in PyPI while for example, the packages "django" and "pillow" exist in PyPI:
pip install django
pip install pillow
So, to install the packages listed in "requirements.txt", you must need "-r";
pip install -r requirements.txt
You can check what "-r" means by running the command below:
pip install --help
-r, --requirement Install from the given requirements file. This option can be used multiple times.
In your case pip install -r requirements.txt will install the libraries listed in your requirements.txt file.
pip install requirements.txt
Above statement looks for a python package named requirements.txt. No such package exists. Your intention is that pip install opens the txt and reads the packages from there. The -r allows pip install to open requirements.txt and install the packages inside of it instead.

How can I define multiple requirement files?

How can I do define multiple requirements files in my requirements.txt file.
-r base.txt
-r test.txt
The current behavior is that pip only installs packages from test.txt. I'd expect pip to install packages found in both base.txt and test.txt. I could have sworn I've seen someone do this in the past, but I can't find any examples.
pip accepts multiple -r arguments:
pip install -r reqs1.txt -r reqs2.txt
The help for pip install says:
-r, --requirement
Install from the given requirements file. This option can be used multiple times.
You can have one file "include" the other; for example, if you put this in file2.txt:
-r file1.txt
Django
Flask
etc.
Then when you do pip install -r file2.txt, it will also install things from file1.txt.
I often use this strategy to have a "base" requirements file, and then only specify those things that are required at each stage (development, testing, staging, production, etc.)
I have many requirements in different directories and solve this problem as:
sudo find . -name "requirement*" -type f -exec pip3 install -r '{}' ';'

PIP: Installing only the dependencies

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.
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/
In my package root issuing pip install -e . installs dependencies.
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`
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? :)
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

Categories