Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an interface to access pyproject.toml from Python?

Is there an interface to access the information in pyproject.toml from Python?

In particular, I'd like to access the dependencies. It doesn't seem hard to do

toml.load("pyproject.toml")['project']['dependencies']

and then parse that list. But that would be rewriting logic that must already be available somewhere.

Apparently there is importlib.metadata.requires(), which would be better than the line above, but that seems to give a similar list of strings that still requires manual parsing:

>>> metadata.requires("scipy")
['numpy<1.27.0,>=1.19.5', 'pytest; extra == "test"', ...

(As for the XY problem, I was thinking about writing a test that tries to install a package with the lowest versions of its dependencies to see whether the package still works.)

like image 764
BlackShift Avatar asked Jun 25 '26 00:06

BlackShift


1 Answers

In both cases, whether project metadata (such as dependency requirements) is obtained from pyproject.toml's standardized [project] section or via importlib.metadata the values should be the same. At least, as far as I know, the dependency requirements have the same format.

Probably I would use the 3rd party packaging library, in particular its packaging.requirements.Requirement(), to parse the dependency requirements. This library can handle other values of project metadata as well, read the documentation. As far as I know this is the library that pip itself uses internally (so it should be always reliable and up-to-date).

The issue with obtaining project metadata from pyproject.toml, is that some fields can be declared dynamic. Which means their value is not set in pyproject.toml.

If you are fine with that, then the TOML parser from Python's standard library tomllib could be good enough. You could also give pyproject-parser a try, it seems like a correct and reliable library.

like image 151
sinoroc Avatar answered Jun 26 '26 16:06

sinoroc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!