25

So today I did found out that with the release of pip 10.x.x the req package changed its directory and can now be found under pip._internal.req.

Since it is common practice to use the parse_requirements function in your setup.py to install all the dependencies out of a requirements file I now wonder if this practice should change since it is now lying under _internal?

Or what is actually best practice without using parse_requirements?

muthan
  • 2,342
  • 4
  • 20
  • 32
  • 5
    It may be a common practice, but I don't think it ever was a best practice. I've always done it the other way around and had `requirements.txt` reference `.` instead. – Daniel Pryden Apr 06 '18 at 09:52
  • Possible duplicate of [Reference requirements.txt for the install\_requires kwarg in setuptools setup.py file?](https://stackoverflow.com/questions/14399534/reference-requirements-txt-for-the-install-requires-kwarg-in-setuptools-setup-py) – Daniel Pryden Apr 06 '18 at 09:56
  • 1
    https://github.com/pypa/pip/issues/2286#issuecomment-68285791 – Stop harming Monica Apr 06 '18 at 09:59
  • @DanielPryden I guessed so. How would that look like? – muthan Apr 06 '18 at 10:00
  • 1
    Thank you for your input. I found a code sample. https://github.com/NordicSemiconductor/pc-nrfutil/pull/94/files – muthan Apr 06 '18 at 11:04

6 Answers6

28

First, I believe parsing requirements.txt to fill the list of dependencies in package metadata is not a good idea. The requirements.txt file and the list of "install dependencies" are two different concepts, they are not interchangeable. It should be the other way around, the list of dependencies in package metadata should be considered as some kind of source of truth, and files such as requirements.txt should be generated from there. For example with a tool such as pip-compile. See the notes at the bottom of this answer.

But everyone has different needs, that lead to different workflows. So with that said... There are 3 possibilities to handle this, depending on where you want your project's package metadata to be written: pyproject.toml, setup.cfg, or setup.py.


Words of caution!

If you insist on having the list of dependencies in package metadata be read from a requirements.txt file then make sure that this requirements.txt file is included in the "source distribution" (sdist) otherwise installation will fail, for obvious reasons.

These techniques will work only for simple requirements.txt files. See Requirements parsing in the documentation page for pkg_resources to get details about what is handled. In short, each line should be a valid PEP 508 requirement. Notations that are really specific to pip are not supported and it will cause a failure.


pyproject.toml

[project]
# ...
dynamic = ["dependencies"]

[tool.setuptools.dynamic]
# ...
dependencies = requirements.txt

setup.cfg

Since setuptools version 62.6 it is possible to write something like this in setup.cfg:

[options]
install_requires = file: requirements.txt

setup.py

It is possible to parse a relatively simple requirements.txt file from a setuptools setup.py script without pip. The setuptools project already contains necessary tools in its top level package pkg_resources.

It could more or less look like this:

#!/usr/bin/env python

import pathlib

import pkg_resources
import setuptools

with pathlib.Path('requirements.txt').open() as requirements_txt:
    install_requires = [
        str(requirement)
        for requirement
        in pkg_resources.parse_requirements(requirements_txt)
    ]

setuptools.setup(
    install_requires=install_requires,
)

Notes:

sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • 4
    This doesn't handle complex entries like `-r extra_req.txt`. The options importing from pip do solve these cases, but they have other problems. – AlanSE Jul 27 '20 at 20:46
  • Yes, it is true. The answer states "_parse a relatively simple `requirements.txt` file_" on purpose, it could be more explicit about it. – sinoroc Jul 27 '20 at 21:02
  • 1
    Note that while the "Requirements parsing" link doesn't explicitly say, it does appear to handle comments in the requirements.txt file. – pavon Apr 01 '21 at 20:12
  • more or less..? – jtlz2 Jul 30 '21 at 07:17
  • 1
    @jtlz2 "more or less" because the `setup.py` needs to be adapted to the rest of the project's needs, and most likely it will not be enough to just copy-paste this example into your own project, some adjustments are probably need. – sinoroc Dec 29 '21 at 09:06
  • Use of pkg_resources is discouraged in favor of importlib.resources, importlib.metadata. Source: https://setuptools.pypa.io/en/latest/pkg_resources.html – Georgios Syngouroglou Feb 02 '23 at 14:35
  • @GeorgiosSyngouroglou From my point of view, this note is irrelevant here for now. The features of `pkg_resources` we use here is not available in `importlib` and will most likely stay in `pkg_resources` for the foreseeable future. Anyway this can be done directly in `setup.cfg` and `pyproject.toml` now without needing a `setup.py` that imports `pkg_resources`. – sinoroc Feb 16 '23 at 10:16
11

The solution of Scrotch only works until pip 19.0.3, in the pip >= 20 versions the PipSession module was refactored. Here is a solution for the imports that works for all pip versions:

try:
    # pip >=20
    from pip._internal.network.session import PipSession
    from pip._internal.req import parse_requirements
except ImportError:
    try:
        # 10.0.0 <= pip <= 19.3.1
        from pip._internal.download import PipSession
        from pip._internal.req import parse_requirements
    except ImportError:
        # pip <= 9.0.3
        from pip.download import PipSession
        from pip.req import parse_requirements
sh0rtcircuit
  • 445
  • 3
  • 13
  • 2
    The solution of @sinoroc is clean. However, if using tools like `pip-tools` to create your `requirements.txt` file, items such as `--trusted-host` may be introduced in the `requirements.txt` file, which the `pkg_resources` package fails to parse. – sh0rtcircuit Jan 31 '20 at 12:25
  • 1
    Haven't tested it, but I have no doubt you're right. When working with tools such as _pip-tools_, then I don't see the point of parsing `requirements.txt` from within `setup.py`, the tools should read `setup.py` or `setup.cfg` to generate `requirements.txt`, not the the other way around. But I guess everyone has their own needs for different workflows. – sinoroc Apr 08 '20 at 09:18
  • This is less useful than what it looks like. Presumably you want to actually use it, like `[r.requirement for r in parse_requirements(filename, session=PipSession())]`. That works for pip >=20, but does not work for 9.0.3. Does anyone have a cross-version compatible way of getting the lines back out of this? In any form. – AlanSE Jul 30 '20 at 15:19
  • Just use pkg_resources – Brian Peterson Dec 30 '22 at 19:00
8

EDIT: modified to support pip>= 19.0.3

I don't agree with the accepted answer. The setup.py file can get ugly real fast if you have a large project with a lot of dependencies. It is always good practice to keep your requirements in a separate .txt file. I would do something like this -

try:
    # pip >=20
    from pip._internal.network.session import PipSession
    from pip._internal.req import parse_requirements
except ImportError:
    try:
        # 10.0.0 <= pip <= 19.3.1
        from pip._internal.download import PipSession
        from pip._internal.req import parse_requirements
    except ImportError:
        # pip <= 9.0.3
        from pip.download import PipSession
        from pip.req import parse_requirements

requirements = parse_requirements(os.path.join(os.path.dirname(__file__), 'requirements.txt'), session=PipSession())

if __name__ == '__main__':
    setup(
        ...
        install_requires=[str(requirement.requirement) for requirement in requirements],
        ...
    )

Throw in all your requirements in requirements.txt under project root directory.

sbrk
  • 1,338
  • 1
  • 17
  • 25
  • 4
    I mean how you load the list of packages into your setup.py is a complete other topic. You can import that from another file that lives somewhere else. But i still think it is wrong to be dependend on the pip library in your own application. If you dont want to have the package list inside your setup.py put it in any other file put it into a constant and import that into your setup.py – muthan Jul 24 '19 at 22:12
5

What I figured out the right way to do is adding the dependencies in the setup.py like:

REQUIRED_PACKAGES = [
    'boto3==1.7.33'
]

if __name__ == '__main__':
    setup(
        ...
        install_requires=REQUIRED_PACKAGES,
        ...
    )

and just have a . in your requirements.txt. It will then automatically install all packages from the setup.py if you install from the file.

muthan
  • 2,342
  • 4
  • 20
  • 32
0

@sinoroc is correct that you generally do not want to populate your setup() function using your requirements.txt. They are for different things: requirements.txt produces a maximal, reproducible environment with hard versions to aid in deployments, help other developers etc.. Setup dependencies are a minimal, permissive list to allow users to install things.

However, sometimes you want to install an application using pip, or collaborators insist on using the requirements file, so I wrote a package which helps you do this: https://pypi.org/project/extreqs/

Chris L. Barnes
  • 475
  • 4
  • 13
-6
with open("requirements.txt") as f:
    dependencies = [line for line in f if "==" in line]

setup(
    install_requires=dependencies
)
James H
  • 531
  • 6
  • 15
  • 2
    There's a lot more to `requirements.txt` syntax than just `==`. – DeusXMachina Jul 14 '21 at 18:10
  • See: https://jmchilton-galaxy.readthedocs.io/en/latest/_modules/pkg_resources.html#parse_requirements for some of the bases you would need to cover if you rolled your own. But also, you can just use pkg_resources – Brian Peterson Dec 30 '22 at 19:02