Can pip install packages recursively from requirements.txt - python

I have a projectA that depends on other projects. Some of them also my projects from private git repository. I listed all dependencies of projectA in requirements.txt for all my packages.
Now projectB that projectA depends of have dependencies also (listed in requirements txt and setup.py), but pip doesn't install them when I'm running
pip install -r requirements.txt  for projectA.

snakebasket appears to be a wrapper of pip with exactly this design goal.

Related

What is the true equivalent of package.json for pip?

I know about requirements.txt, but that only includes a list of dependencies.
But what about the other meta information like the package name, author, main function etc. ?
Also i know about setup.py but since i want to programmatically access values inside, i need a configuaration file standard like yaml/json and not python code.
Did the python community come out with something truly comparable to package.json ?
1. Without 3rd Party Packages
pip freeze > requirements.txt
In the local machine. And in the server,
pip install -r requirements.txt
This installs all the dependencies
2. With a 3rd Party Package
pipenv
I would use pipenv instead of pip. pipenv automatically generate Pipfile and Pipfile.lock that is far superior to requirements.txt
Install pipenv and setting it for your project
pip install --user pipenv
cd yourproject
pipenv install package1 package2 ...
to install packages from Pipfile is as simple as
pipenv install
Read more: https://pipenv.pypa.io/en/latest/
poetry
I have recently moved from pipenv to poetry because poetry has everything pipenv offers and much more. It is end-to-end, as it includes building and publishing of your project to pypi.
installing poetry
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
and set .poetry/bin in your path.
poetry new yourproject
cd yourproject
poetry add packagename
Like pipenv this generate pyproject.toml file that context all your requirements. Like Pipenv, to install your depence
poetry install
See more: https://poetry.eustace.io/docs/
See Python packaging war: Pipenv vs. Poetry for short review of these awesome packages
Like the comments on my question answered, it's pyproject.toml together with the poetry package management tool

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

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

How to install package via pip requirements.txt from VCS into current directory?

For example, we have project Foo with dependency Bar (that in private Git repo) and we want install Bar into Foo directory via pip from requirements.txt.
We can manually install Bar with console command:
pip install --target=. git+ssh://git.repo/some_pkg.git#egg=SomePackage
But how to install Bar into current directory from requirements.txt?
The best way to do this would be to clone the repository, or just donwload the requirements.txt file, and then run pip install -r requirements.txt to install all the modules dependencies.
You can use the "editable" syntax in requirements.txt to install a package from a VCS (eg git)
From the docs:
pip install -e git+https://git.repo/some_pkg.git#egg=SomeProject

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