pip installable package with alternative index in requirements.txt - python

Along the lines of pip requirements.txt with alternative index (see also this) I have to install a version of a package that requires passing an --extra-index-url and I want to do this inside requirements.txt. This works if I run pip install -r requirements.txt.
The kicker is that I want my package to be pip installable. I have the requirements.txt of my package inside a directory with the other necessary files like setup.py and the pyproject.toml and whilst I can run the previous command I can't do pip install . to install my package locally from the current directory.
How can I ship a pip installable package with a requirements.txt file that contains an extra index URL?
(If pip doesn't allow this as I suspect, please suggest workarounds!)
Right now I have this in the requirements file (just an excerpt):
seaborn==0.11.2
--extra-index-url https://download.pytorch.org/whl/cu113
torch==1.10.2+cu113
pandas==1.1.5
numpy==1.17.4
My specific use case is with PyTorch, which I need to install with the correct CUDA version, so the command to install the relevant packages from PyTorch Start Locally is:
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113

Related

How to add git source in requirements.txt

I would like to install a certain package from a private git repository. This is possible using pip install git+<REPO_LINK>. However, I would like to pip install -r requirements.txt all of my packages at the same time without having to specify which one comes from Pypi and private repo.
I have tried adding a configuration in ~/.config/pip/pip.conf
[global]
find-links =
git+<REPO_LINK>
but this happened when running pip install -r requirements.txt:
ERROR: Could not find a version that satisfies the requirement my-package==0.1
Thanks in advance.
I have found a solution for it at this doc.
pip install git+<REPO_LINK>#egg=<PACKAGE_NAME>
When I run pip freeze, the package I have just installed is printed like this:
git+<REPO_LINK>#egg=<PACKAGE_NAME>
Add it to your requirements.txt so pip install -r requirements.txt install this specific package so as public ones from Pypi.
:)

Why use pip install requirements.txt instead of pip install ./?

I'm not very experienced with dependency management in Python but it seems that all of the dependencies in requirements.txt could alternatively be placed in the setup.py file under the install_requires field as follows:
setup(
...
install_requires=['numpy=1.2.3', 'pandas=1.2.3']
...
)
Since pip install ./ using a setup.py file also provides lots of additional functionality compared to pip install -r requirements.txt, what is the use for the latter? Would a project ever have a valid reason to use both?
pip install . installs your custom package with all its dependencies. pip install -r requirements.txt installs only the dependencies.
You could also add -e . to your requirements.txt and install your package with pip install -r requirements.txt

Python update package version in requirements.txt

I have a requirement.txt file with the list of python package to install. One of the packages is psycopg2==2.6.2 I need to update this package to psycopg2==2.7. I tried to install by pip3 install psycopg2 But it doesn't affect requirement.txt file. Can you please point me in the right direction?
Notice that running pip3 install psycopg2 doesn't respect the requirements.txt file. To upgrade this package you need to use -U option:
pip3 install -U psycopg2
which is a shorthand for:
pip3 install --upgrade psycopg2
After that, you can update your requirements.txt with the following command:
pip freeze > requirements.txt
If you're looking for a solution to automatically update the requirements.txt file after you upgrade package/packages, you can use pip-upgrader.
Installation:
pip install pip-upgrader
Usage:
pip-upgrade
The above command auto-discovers the requirements file and prompts for selecting upgrades. You can also specify a path to the requirements file or/and specify a package to upgrade:
pip-upgrade /path/to/requirements.txt -p psycopg2
As you've discovered, pip doesn't update the requirements file. So the workflow you'd likely want to use is:
Update the version of psycopg2 in your requirements file from 2.6.2 to 2.7
Run pip install with the upgrade flag
pip3 install -U -r requirements.txt
If you're familiar with tools like npm that do update the version in the catalog file, you may be interested in using pipenv, which manages your dependencies and the virtual environment for you, much like npm does.
If you don't know the latest version of your package, then use pip to figure it out:
$ pip list --outdated | grep psycopg2
psycopg2 (2.7.3.2) - Latest: 2.7.4 [wheel]
you can try:
pip install --upgrade --force-reinstall -r requirements.txt
You can also ignore installed package and install the new one :
pip install --ignore-installed -r requirements.txt

How to install package using pip and save name to file to install it later

I'd like to do something like pip install requests --save packages.txt so I could have list of all I used and later I could just pip -r install packages.txt when I clone it from repository.
you can use freeze to dump all the installations to your .txt file as:
pip freeze > requirements.txt
And, you can run following later when needed :
pip install -r requirements.txt
pip install package --download="path to directory"
pip install --no-index --find-links="path to directory" package_name
Note: pip download replaces the --download option to pip install, which is now deprecated and will be removed in pip 10.

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.

Categories