How to add git source in requirements.txt - python

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.
:)

Related

pip installable package with alternative index in requirements.txt

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

Downloading requirements.txt from GitHub

I trying to download requirements.txt from my repo in GitHub
I use pip install -e git+https://github.com/GabrielCoutz/Problema-Chiado#egg=requirements.txt and this is returning:
ERROR: File "setup.py" not found for legacy project requirements.txt from git+https://github.com/GabrielCoutz/Problema-Chiado#egg=requirements.txt.
How i can create setup.py and what i have to put in?
pip install -r https://raw.githubusercontent.com/GabrielCoutz/Problema-Chiado/main/requirements.txt
-e is for local installs for developing, for example. Check this:
What is the use case for `pip install -e`?

pip force reinstall in requirements.txt

I have a requirements.txt file in which I have some git+ references. I would like to always reinstall these as for some reason, even if I make changes and bump the version and push it to my github repo, pip says requirements already satisfied and doesn't install.
Here is part of my requirements.txt file:-
Django==1.10
git+https://github.com/myaccount/myrepo.git#master#egg=some_egg
I don't want to reinstall everything in the requirements.txt file. Only the git+ requirements.
I tried this:-
git+https://github.com/myaccount/myrepo.git#master#egg=some_egg --install-option="--upgrade --ignore-installed --force-reinstall"
But none of the above options worked.
The problem is that you haven't adviced pip what version do you have in git:
git+https://github.com/myaccount/myrepo.git#master#egg=some_egg
For VCS URLs pip doesn't look into the repo to find out the version, it only look at the URL:
git+https://github.com/myaccount/myrepo.git#master#egg=some_egg-version
example:
git+https://github.com/myaccount/myrepo.git#master#egg=package-1.0.8
When you push a new version to Github update your requirements.txt with new version(s) and run pip install -r requirements.txt -U.
Probably one option is to install the package in editable mode, like
Django==1.10
-e git+https://github.com/myaccount/myrepo.git#master#egg=some_egg
Pip developers stated in 2017 that they don't want you to be able to force reinstall in requirements.txt, although I don't think they explained why.
I use this:
pip install -r requirements.txt
And you can use some thing more like :
pip install -r requirements.txt --no-index --find-links
--no-index - Ignore package index (only looking at --find-links URLs instead).
-f, --find-links <URL> - If a URL or path to an html file, then parse for links to archives

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

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