Alternative: pip.req
Opened this issue · 1 comments
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
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
:
Is subtly different than that returned by requirements-parser
:
Apart from that, they seem pretty comparable?