python / importlib_metadata

Library to access metadata for Python packages
https://importlib-metadata.readthedocs.io
Apache License 2.0
122 stars 79 forks source link

Question: parsing package names & versions from METADATA file instead of dist-info directory #476

Closed lada-gagina closed 7 months ago

lada-gagina commented 8 months ago

Hello! Sorry if that’s a stupid question, please direct me if it should be asked in some different place. Python 3.12 has dropped pkg_resources module which was providing access to installed packages info. The alternative is importlib.metadata. I see that the latter works differently: it seems to use METADATA file inside dist-info dir, instead of parsing the package’s name and version directly from dist-info dir’s name. I see it’s most probably intended, but why does it work this way now?

jaraco commented 7 months ago

Hi Lada-Gagina. I'm happy to answer questions. I do only check Github intermittently, so that's why there's sometimes a delay.

Although there are some cases where pkg_resources loads the version from the filename, there are other places where it loads the version from the metadata.

There are some cases where a version number isn't available or accurate in the metadata file, so importlib metadata attempts to rely on the metadata as the canonical source for all metadata, including version. Another reason for not relying on the filename is that the Python import system offers extensible interfaces for package providers such that the packages and their metadata can exist without a file system at all, where the metadata could be stored in a database for example.

If I had to guess, I expect the reason that pkg_resources relied on the version in the filename was for efficiency - if one can assume the version is in the filename, it can save many stat and read operations to extract the version from the metadata. Unfortunately, that's just not a reliable source of truth, so it's no longer used.

I hope that answers the question. I'll close this for now, but feel free to follow-up if needed.