So the workaround I started with was to try for tomllib
and, failing that, import the vendored version as mentioned by wim:
try: import tomllib
except ModuleNotFoundError: import pip._vendor.tomli as tomllib
Since the APIs are the same (or close enough for what I needed), this worked well with no further code changes. I also confirmed that a comment above mentioning that it was pip._vendor.toml
in Python 3.8 was incorrect; it's tomli
, as it is in all the others.
However, this works with the "modern" version of pip, which is only usable on 3.8 and above. Python 3.7 and below have their own separate versions of getpip.py
, and 3.6 does not seem to have a vendored TOML library in it. (3.7 i believe does.)
Since in my particular case I was just using this to do a tiny bit of parsing on pyproject.toml
files before installing those packages and their dependencies, and I am always using a virtual environment for this anyway, I decided the best option was to keep things simple: directly install the tomli
package from PyPI and use only it, saving myself the hassle of multiple different checks.
But if you're supporting only Python 3.7 and up, the above might be one of the easiest solutions.
pip
has access to a parser; it doesn't really provide it, in the sense that you are welcome to use it. – Consequentlypip
is only for its internal use. I still like knowing because (a) MacGyver, and (b) I learn more about how python packages are managed and how "batteries included" is achieved. – Zenobiapip
may stop providing a vendored parser in the future. – Consequentlypip
maintainers may be planning.) – Consequently