madpah / requirements-parser

A Pip requirements file parser.
https://requirements-parser.readthedocs.io
Apache License 2.0
125 stars 41 forks source link

Alternative: pip.req #50

Open kousu opened 4 years ago

kousu commented 4 years ago

It looks like this used to be a feature of pip itself: https://stackoverflow.com/questions/25192794/no-module-named-pip-req#49867265

You can still use it with

try: # for pip >= 10
    from pip._internal.req import parse_requirements
except ImportError: # for pip <= 9.0.3
    from pip.req import parse_requirements
joshuacwnewton commented 1 year ago

I tried the pip._internal version of parse_requirements, but the function required a PipSession object, which felt like a sign that I was doing something I shouldn't (re: accessing internal functions/methods).

I then poked around and found https://stackoverflow.com/a/59971236, which suggests pkg_resources.parse_requirements. This seems like it might be the proper way of accessing the "official" user-facing requirements parser for pip/setuptools?

There are some differences worth noting, though:

Handling of non-requirement specifications (e.g. --extra-url) This line: ``` --extra-index-url https://download.pytorch.org/whl/cpu # append_to_freeze ``` Triggers the following traceback: ``` Traceback (most recent call last): File "/home/joshua/repos/spinalcordtoolbox/python/envs/venv_sct/lib/python3.8/site-packages/pkg_resources/_vendor/packaging/requirements.py", line 102, in __init__ req = REQUIREMENT.parseString(requirement_string) File "/home/joshua/repos/spinalcordtoolbox/python/envs/venv_sct/lib/python3.8/site-packages/pkg_resources/_vendor/pyparsing/core.py", line 1141, in parse_string raise exc.with_traceback(None) pkg_resources._vendor.pyparsing.exceptions.ParseException: Expected W:(0-9A-Za-z), found '-' (at char 0), (line:1, col:1) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/joshua/repos/spinalcordtoolbox/python/envs/venv_sct/lib/python3.8/site-packages/pkg_resources/_vendor/packaging/requirements.py", line 104, in __init__ raise InvalidRequirement( pkg_resources.extern.packaging.requirements.InvalidRequirement: Parse error at "'--extra-'": Expected W:(0-9A-Za-z) ``` While `requirements-parser` (correctly) skips over this line just fine.
Requirement objects The signature of the `Requirement` object returned by `pkg_resources`: ![image](https://user-images.githubusercontent.com/16181459/213885971-2c26841b-101d-409f-824f-0d55dd26a2d9.png) Is subtly different than that returned by `requirements-parser`: ![image](https://user-images.githubusercontent.com/16181459/213885949-00077930-b0ea-4b85-bbc2-a30d3a576c41.png)

Apart from that, they seem pretty comparable?