Movatterモバイル変換


[0]ホーム

URL:


ContentsMenuExpandLight modeDark modeAuto light/dark, in light modeAuto light/dark, in dark modeSkip to content
importlib_metadata 8.7.2.dev1+gd8a7576de documentation
importlib_metadata 8.7.2.dev1+gd8a7576de documentation

Links

Back to top

API Reference

importlib_metadata module

APIs exposing metadata from third-party Python packages.

This codebase is shared between importlib.metadata in the stdliband importlib_metadata in PyPI. Seehttps://github.com/python/importlib_metadata/wiki/Development-Methodologyfor more detail.

classimportlib_metadata.Distribution

Bases:object

An abstract Python distribution package.

Custom providers may derive from this class and definethe abstract methods to provide a concrete implementationfor their environment. Some providers may opt to overridethe default implementation of some properties to bypassthe file-reading mechanism.

staticat(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

classmethoddiscover(*,context:Context|None=None,**kwargs)Iterable[Distribution]

Return an iterable of Distribution objects for all packages.

Pass acontext or pass keyword arguments for constructinga context.

Context:

ADistributionFinder.Context object.

Returns:

Iterable of Distribution objects for packages matchingthe context.

propertyentry_points:EntryPoints

Return EntryPoints for this distribution.

Custom providers may provide theentry_points.txt fileor override this property.

propertyfiles:list[PackagePath]|None

Files in this distribution.

Returns:

List of PackagePath for this distribution or None

Result isNone if the metadata file that enumerates files(i.e. RECORD for dist-info, or installed-files.txt orSOURCES.txt for egg-info) is missing.Result may be empty if the metadata exists but is empty.

Custom providers are recommended to provide a “RECORD” file (inread_text) or override this property to allow for callers to beable to resolve filenames provided by the package.

classmethodfrom_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 namedpackage, if found.

Raises:
abstractmethodlocate_file(path:str|PathLike[str])SimplePath

Given a path to a file in this distribution, return a SimplePathto it.

This method is used by callers ofDistribution.files() tolocate files within the distribution. If it’s possible for aDistribution to represent files in the distribution asSimplePath objects, it should implement this methodto resolve such objects.

Some Distribution providers may elect not to resolve SimplePathobjects within the distribution by raising aNotImplementedError, but consumers of such a Distribution wouldbe unable to invokeDistribution.files().

propertymetadata:PackageMetadata|None

Return the parsed metadata for this Distribution.

The returned object will have keys that name the various bits ofmetadata per theCore metadata specifications.

Custom providers may provide the METADATA file or override thisproperty.

propertyname:str

Return the ‘Name’ metadata for the distribution package.

propertyorigin
abstractmethodread_text(filename)str|None

Attempt to load metadata file given by the name.

Python distribution metadata is organized by blobs of texttypically represented as “files” in the metadata directory(e.g. package-1.0.dist-info). These files include thingslike:

  • METADATA: The distribution metadata including fieldslike Name and Version and Description.

  • entry_points.txt: A series of entry points as defined inthe entry points spec.

  • RECORD: A record of files according tothis recording spec.

A package may provide any set of files, including thosenot listed here or none at all.

Parameters:

filename – The name of the file in the distribution info.

Returns:

The text if found, otherwise None.

propertyrequires:list[str]|None

Generated requirements specified for this Distribution

propertyversion:str

Return the ‘Version’ metadata for the distribution package.

classimportlib_metadata.DistributionFinder

Bases:MetaPathFinder

A MetaPathFinder capable of discovering installed distributions.

Custom providers should implement this interface in order tosupply metadata.

classContext(**kwargs)

Bases:object

Keyword arguments presented by the caller todistributions() orDistribution.discover()to narrow the scope of a search for distributionsin all DistributionFinders.

Each DistributionFinder may expect any parametersand should attempt to honor the canonicalparameters defined below when appropriate.

This mechanism gives a custom provider a means tosolicit additional details from the caller beyond“name” and “path” when searching distributions.For example, imagine a provider that exposes suitesof packages in either a “public” or “private”realm.A caller may wish to query only for distributions ina particular realm and could calldistributions(realm="private") to signal to thecustom provider to only include distributions from thatrealm.

name=None

Specific name for which a distribution finder should match.A name ofNone matches all distributions.

propertypath:list[str]

The sequence of directory path that a distribution findershould search.

Typically refers to Python installed package paths such as“site-packages” directories and defaults tosys.path.

abstractmethodfind_distributions(context=Context())Iterable[Distribution]

Find distributions.

Return an iterable of all Distribution instances capable ofloading the metadata for packages matching thecontext,a DistributionFinder.Context instance.

classimportlib_metadata.PackageMetadata(*args,**kwargs)

Bases:Protocol

get(**kwds)

Helper for @overload to raise when called.

get_all(**kwds)

Helper for @overload to raise when called.

propertyjson:dict[str,str|list[str]]

A JSON-compatible form of the metadata.

exceptionimportlib_metadata.PackageNotFoundError

Bases:ModuleNotFoundError

The package was not found.

propertyname:str

module name

classimportlib_metadata.SimplePath(*args,**kwargs)

Bases:Protocol

A minimal subset of pathlib.Path required by Distribution.

exists()bool
joinpath(other:str|PathLike[str])SimplePath
propertyparent:SimplePath
read_bytes()bytes
read_text(encoding=None)str
importlib_metadata.distribution(distribution_name:str)Distribution

Get theDistribution instance for the named package.

Parameters:

distribution_name – The name of the distribution package as a string.

Returns:

ADistribution instance (or subclass thereof).

importlib_metadata.distributions(**kwargs)Iterable[Distribution]

Get allDistribution instances in the current environment.

Returns:

An iterable ofDistribution instances.

importlib_metadata.entry_points(**params)EntryPoints

Return EntryPoint objects for all installed packages.

Pass selection parameters (group or name) to filter theresult to entry points matching those properties (seeEntryPoints.select()).

Returns:

EntryPoints for all installed packages.

importlib_metadata.files(distribution_name:str)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:str)PackageMetadata|None

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 theirdistributions.

>>>importcollections.abc>>>pkgs=packages_distributions()>>>all(isinstance(dist,collections.abc.Sequence)fordistinpkgs.values())True
importlib_metadata.requires(distribution_name:str)list[str]|None

Return a list of requirements for the named package.

Returns:

An iterable of requirements, suitable forpackaging.requirement.Requirement.

importlib_metadata.version(distribution_name:str)str

Get the version string for the named package.

Parameters:

distribution_name – The name of the distribution package to query.

Returns:

The version string for the package as defined in the package’s“Version” metadata key.

classimportlib_metadata._meta.PackageMetadata(*args,**kwargs)

Bases:Protocol

get(**kwds)

Helper for @overload to raise when called.

get_all(**kwds)

Helper for @overload to raise when called.

propertyjson:dict[str,str|list[str]]

A JSON-compatible form of the metadata.

classimportlib_metadata._meta.SimplePath(*args,**kwargs)

Bases:Protocol

A minimal subset of pathlib.Path required by Distribution.

exists()bool
joinpath(other:str|PathLike[str])SimplePath
propertyparent:SimplePath
read_bytes()bytes
read_text(encoding=None)str
On this page

[8]ページ先頭

©2009-2026 Movatter.jp