I want to parse TOML files in python 3.9, and I am wondering if I can do so without installing another package.
Since pip knows how to work with pyproject.toml files, and I already have pip installed, does pip provide a parser that I can import and use in my own code?
For 3.9, pip vendors tomli:
from pip._vendor import tomli
For consenting adults, importing the vendored module should work as usual. However, this is an implementation detail of pip, and it could change without a deprecation period at any moment in a future release of pip. Therefore, for anything apart from a quick hack, it would be safer to install tomli (or some other TOML parser) into the site instead.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With