PEP 610 metadata availability in Python

I am interested in accessing PEP 610 metadata from Python. Is there an API for doing that?

My immediate interest was in determining if a package is installed in editable mode, but I could see wanting to get detailed version information about a VCS installed package. From looking at how pip determines this by loading and parsing direct_url.json directly, I am guessing there is no API. Also, based on that code, I see I would have to handle egg-link files (or just hope that they fall out of use over time).

Would it make sense for the direct_url.json metadata to be available from importlib.metadata, or another package?

>>> d = distribution("pip")
>>> d.read_text("direct_url.json")
'{"dir_info": {"editable": true}, "url": "file:///C:/Work/Projects/pip"}'

You can simply use the json module to parse it. And read_text will return None if there’s no direct_url.json file.

Edit: The pip code you linked to is doing something rather different, which is why it doesn’t use the approach I give here.

3 Likes

Would it make sense for the direct_url.json metadata to be available from importlib.metadata, or another package?

I once considered moving pip’s direct_url.py to a standalone library but decided it was not worth the hassle for now, as parsing and processing the json is easy enough.

I see I would have to handle egg-link files

If you need to handle legacy editable installs, you may want to delegate the hairy details to pip, using the pip inspect command, which will emulate a direct_url for you.

2 Likes

Thanks! I will use Distribution.read_text() and json.loads(). It is easy enough. I just wasn’t sure what the scope of importlib.metadata was regarding the package metadata files.