The following guide will help you migrate commonpkg_resourcesAPIs toimportlib_metadata.importlib_metadata aims toreplace the followingpkg_resources APIs:
pkg_resources.iter_entry_points()
pkg_resources.require()
convenience functions
pkg_resources.find_distributions()
pkg_resources.get_distribution()
Other functionality frompkg_resources is replaced by otherpackages such asimportlib_resourcesandpackaging.
importlib_metadata providesEntry points.
Compatibility note: entry points provided by importlib_metadatado not have the following implicit behaviors found in thosefrompkg_resources:
Each EntryPoint is not automatically validated to match. Toensure each one is validated, invoke any property on theobject (e.g.ep.name).
When invokingEntryPoint.load(), no checks are performedto ensure the declared extras are installed. If this behavioris desired/required, it is left to the user to perform thecheck and install any dependencies. Seeimportlib_metadata#368for more details.
importlib_metadata does not provide support for dynamicallydiscovering or requiring distributions nor does it provide anysupport for managing the “working set”. Furthermore,importlib_metadata assumes that only one version of a givendistribution is discoverable at any time (no support for multi-versioninstalls). Any projects that require the above behavior needs toprovide that behavior independently.
importlib_metadata does aim to resolve metadata concerns latesuch that any dynamic changes to package availability should bereflected immediately.
In addition to the support for direct access toDistributionobjects (below),importlib_metadata presents some top-levelfunctions for easy access to the most common metadata:
Distribution metadata queries the metadata fields from the distribution.
Distribution versions provides quick access to the distribution version.
Distribution requirements presents the requirements of the distribution.
Distribution files provides file-like access to the data blobs backingthe metadata.
importlib_metadata provides functionalitysimilar tofind_distributions(). Bothdistributions(...) andDistribution.discover(...) return an iterable ofDistributionsmatching the indicated parameters.
Similar todistributions, thedistribution() function providesaccess to a single distribution by name.