API Reference#
importlib_metadata
module#
- class importlib_metadata.Distribution(*args, **kwargs)#
Bases:
DeprecatedNonAbstract
A Python distribution package.
- static at(path: str | PathLike[str]) Distribution #
Return a Distribution for the indicated metadata path
- Parameters:
path – a string or path-like object
- Returns:
a concrete Distribution instance for the path
- classmethod discover(**kwargs) Iterable[Distribution] #
Return an iterable of Distribution objects for all packages.
Pass a
context
or pass keyword arguments for constructing a context.- Context:
A
DistributionFinder.Context
object.- Returns:
Iterable of Distribution objects for all packages.
- property entry_points: EntryPoints#
- property files: List[PackagePath] | None#
Files in this distribution.
- Returns:
List of PackagePath for this distribution or None
Result is None if the metadata file that enumerates files (i.e. RECORD for dist-info, or installed-files.txt or SOURCES.txt for egg-info) is missing. Result may be empty if the metadata exists but is empty.
- classmethod from_name(name: str) Distribution #
Return the Distribution for the given package name.
- Parameters:
name – The name of the distribution package to search for.
- Returns:
The Distribution instance (or subclass thereof) for the named package, if found.
- Raises:
PackageNotFoundError – When the named package’s distribution metadata cannot be found.
ValueError – When an invalid value is supplied for name.
- abstract locate_file(path: str | PathLike[str]) Path #
Given a path to a file in this distribution, return a path to it.
- property metadata: PackageMetadata#
Return the parsed metadata for this Distribution.
The returned object will have keys that name the various bits of metadata. See PEP 566 for details.
- class importlib_metadata.DistributionFinder#
Bases:
MetaPathFinder
A MetaPathFinder capable of discovering installed distributions.
- class Context(**kwargs)#
Bases:
object
Keyword arguments presented by the caller to
distributions()
orDistribution.discover()
to narrow the scope of a search for distributions in all DistributionFinders.Each DistributionFinder may expect any parameters and should attempt to honor the canonical parameters defined below when appropriate.
- name = None#
Specific name for which a distribution finder should match. A name of
None
matches all distributions.
- abstract find_distributions(context=Context()) Iterable[Distribution] #
Find distributions.
Return an iterable of all Distribution instances capable of loading the metadata for packages matching the
context
, a DistributionFinder.Context instance.
- class importlib_metadata.PackageMetadata(*args, **kwargs)#
Bases:
Protocol
- get(name: str, failobj: None = None) str | None #
- get(name: str, failobj: _T) str | _T
Helper for @overload to raise when called.
- exception importlib_metadata.PackageNotFoundError#
Bases:
ModuleNotFoundError
The package was not found.
- importlib_metadata.distribution(distribution_name) Distribution #
Get the
Distribution
instance for the named package.- Parameters:
distribution_name – The name of the distribution package as a string.
- Returns:
A
Distribution
instance (or subclass thereof).
- importlib_metadata.distributions(**kwargs) Iterable[Distribution] #
Get all
Distribution
instances in the current environment.- Returns:
An iterable of
Distribution
instances.
- importlib_metadata.entry_points(**params) EntryPoints #
Return EntryPoint objects for all installed packages.
Pass selection parameters (group or name) to filter the result to entry points matching those properties (see EntryPoints.select()).
- Returns:
EntryPoints for all installed packages.
- importlib_metadata.files(distribution_name) List[PackagePath] | None #
Return a list of files for the named package.
- Parameters:
distribution_name – The name of the distribution package to query.
- Returns:
List of files composing the distribution.
- importlib_metadata.metadata(distribution_name) PackageMetadata #
Get the metadata for the named package.
- Parameters:
distribution_name – The name of the distribution package to query.
- Returns:
A PackageMetadata containing the parsed metadata.
- importlib_metadata.packages_distributions() Mapping[str, List[str]] #
Return a mapping of top-level packages to their distributions.
>>> import collections.abc >>> pkgs = packages_distributions() >>> all(isinstance(dist, collections.abc.Sequence) for dist in pkgs.values()) True