My goal is to programmatically extract the requirements of the current directory without the optional ones. So far, this is the closest I got:
from build.util import project_wheel_metadata
from pathlib import Path
package_metadata = project_wheel_metadata(Path(".").resolve())
print(package_metadata.get_all("Requires-Dist"))
However, this does include everything under the optional-dependencies
subtable, which I don’t want.
As a result, I’m considering manually loading pyproject.toml
and extracting the dependencies
list from it:
try:
import tomllib
except ImportError:
import tomli as tomllib
with open("pyproject.toml", "rb") as fh:
# Assume PEP 621
reqs = tomllib.load(fh)["project"]["dependencies"]
but I’m not sure if I’m doing something terribly wrong here.
<rant>
If I’m allowed to rant just a little bit, the only reason it only took me half an hour to get to this point is because I’m a Python packaging nerd already - but I’m not even nerd enough, so I’m not sure if there’s a function that perfectly does what I need, or if I’m about to hack something horrible to remove whatever contains the extra ==
string.
If someone could illuminate me, I’ll try to find some time to improve the documentation wherever possible so that nobody else has to go through this rabbit hole, but for that I’d also need to understand which docs to update (official Python docs? packaging user guide? pypa/build? importlib_metadata? something else?). </rant>
In the meantime, I opened an issue on pypa/build about the docs of this function Confusing return type of `build.util.project_wheel_metadata` · Issue #619 · pypa/build · GitHub and any help in achieving my original goal (if you already forgot what it was because you’re crafting a response to my rant, please scroll back to the top of my post again) is more than welcome.