How can I specify optional dependencies in a pip requirements file?
According to the pip documentation this is possible, but the documentation doesn't explain how to do it, and I can't find any examples on the web.
How can I specify optional dependencies in a pip requirements file?
According to the pip documentation this is possible, but the documentation doesn't explain how to do it, and I can't find any examples on the web.
Instead of specifying optional dependencies in the same file as the hard requirements, you can create a optional-requirements.txt
and a requirements.txt
.
To export your current environment's packages into a text file, you can do this:
pip freeze > requirements.txt
If necessary, modify the contents of the requirements.txt to accurately represent your project's dependencies. Then, to install all the packages in this file, run:
pip install -U -r requirements.txt
-U
tells pip
to upgrade packages to the latest version, and -r
tells it to install all packages in requirements.txt.
In 2015 PEP-0508 defined a way to specify optional dependencies in requirements.txt
:
requests[security]
That means that yourpackage
needs requests
for its security option. You can install it as:
pip install yourpackage[security]
extras_require
argument for the setup
function in setuptools
. You can see in the requests setup.py how the security
"extra" is configured. –
Remsen Collecting pytest-cov[tests] ... WARNING: pytest-cov 2.10.0 does not provide the extra 'tests'
, Collecting numba[fast]<0.47 ... WARNING: numba 0.46.0 does not provide the extra 'fast'"
–
Uvarovite pip install "requests[security] @ git+ssh://[email protected]/psf/requests.git"
instead –
Dialectology Instead of specifying optional dependencies in the same file as the hard requirements, you can create a optional-requirements.txt
and a requirements.txt
.
To export your current environment's packages into a text file, you can do this:
pip freeze > requirements.txt
If necessary, modify the contents of the requirements.txt to accurately represent your project's dependencies. Then, to install all the packages in this file, run:
pip install -U -r requirements.txt
-U
tells pip
to upgrade packages to the latest version, and -r
tells it to install all packages in requirements.txt.
You are misunderstanding the documentation; it's not as clear as it could be. The point in the documentation is that with a requirements file you can feel free to specify your full recommended working set of packages, including both necessary dependencies and optional ones.
You can add comments (lines beginning with #) to distinguish the two to humans, but pip makes no distinction. You can also have two requirements files, as Daniel suggests.
© 2022 - 2024 — McMap. All rights reserved.