dist_meta.distributions

Iterate over installed distributions.

Third-party distributions are installed into Python’s site-packages directory with tools such as pip. Distributions must have a *.dist-info directory (as defined by PEP 566) to be discoverable.

Functions:

get_distribution(name[, path])

Returns a Distribution instance for the distribution with the given name.

iter_distributions([path])

Returns an iterator over installed distributions on path.

packages_distributions([path])

Returns a mapping of top-level packages to a list of distributions which provide them.

Classes:

DistributionType()

Abstract base class for Distribution-like objects.

Distribution(name, version, path)

Represents an installed Python distribution.

WheelDistribution(name, version, path, wheel_zip)

Represents a Python distribution in wheel form.

Exceptions:

DistributionNotFoundError

Raised when a distribution cannot be located.

Data:

get_distribution(name, path=None)[source]

Returns a Distribution instance for the distribution with the given name.

Parameters
Return type

Distribution

iter_distributions(path=None)[source]

Returns an iterator over installed distributions on path.

Parameters

path (Optional[Iterable[Union[str, Path, PathLike]]]) – The directories entries to search for distributions in. Default sys.path.

Return type

Iterator[Distribution]

packages_distributions(path=None)[source]

Returns a mapping of top-level packages to a list of distributions which provide them.

The same top-level package may be provided by multiple distributions, especially in the case of namespace packages.

Parameters

path (Optional[Iterable[Union[str, Path, PathLike]]]) – The directories entries to search for distributions in. Default sys.path.

New in version 0.7.0.

Example:

>>> import collections.abc
>>> pkgs = packages_distributions()
>>> all(isinstance(dist, collections.abc.Sequence) for dist in pkgs.values())
True
Return type

Mapping[str, List[str]]

class DistributionType[source]

Abstract base class for Distribution-like objects.

Changed in version 0.3.0: Previously was a Union representing Distribution and WheelDistribution. Now a common base class for those two classes, and custom classes providing the same API

This class implements most of the collections.namedtuple() API. Subclasses must implement _fields (as a tuple of field names) and the tuple interface (specifically __iter__ and __getitem__).

Attributes:

name

The name of the distribution.

version

The version number of the distribution.

_fields

A tuple of field names for the “namedtuple”.

_field_defaults

A mapping of field names to default values.

Methods:

read_file(filename)

Read a file from the *.dist-info directory and return its content.

has_file(filename)

Returns whether the *.dist-info directory contains a file named filename.

_asdict()

Return a new dict which maps field names to their values.

_replace(**kwargs)

Make a new DistributionType object, of the same type as this one, replacing the specified fields with new values.

_make(iterable)

Make a new DistributionType object, of the same type as this one, from a sequence or iterable.

get_entry_points()

Returns a mapping of entry point groups to entry points.

get_metadata()

Returns the content of the *.dist-info/METADATA file.

get_wheel()

Returns the content of the *.dist-info/WHEEL file, or None if the file does not exist.

get_record()

Returns the parsed content of the *.dist-info/RECORD file, or None if the file does not exist.

__repr__()

Returns a string representation of the DistributionType.

name

Type:    str

The name of the distribution. No normalization is performed.

version

Type:    Version

The version number of the distribution.

_fields

Type:    Tuple[str, …]

A tuple of field names for the “namedtuple”.

_field_defaults

Type:    Dict[str, Any]

A mapping of field names to default values.

abstract read_file(filename)[source]

Read a file from the *.dist-info directory and return its content.

Parameters

filename (str)

Return type

str

abstract has_file(filename)[source]

Returns whether the *.dist-info directory contains a file named filename.

Parameters

filename (str)

Return type

bool

_asdict()[source]

Return a new dict which maps field names to their values.

Return type

Dict[str, Any]

_replace(**kwargs)[source]

Make a new DistributionType object, of the same type as this one, replacing the specified fields with new values.

Parameters

iterable

Return type

~_DT

classmethod _make(iterable)[source]

Make a new DistributionType object, of the same type as this one, from a sequence or iterable.

Parameters

iterable

Return type

~_DT

get_entry_points()[source]

Returns a mapping of entry point groups to entry points.

Entry points in the group are contained in a dictionary mapping entry point names to objects.

dist_meta.entry_points.EntryPoint objects can be constructed as follows:

for name, epstr in distro.get_entry_points().get("console_scripts", {}).items():
    EntryPoint(name, epstr)
Return type

Dict[str, Dict[str, str]]

get_metadata()[source]

Returns the content of the *.dist-info/METADATA file.

Return type

MetadataMapping

get_wheel()[source]

Returns the content of the *.dist-info/WHEEL file, or None if the file does not exist.

The file will only be present if the distribution was installed from a wheel.

Return type

Optional[MetadataMapping]

get_record()[source]

Returns the parsed content of the *.dist-info/RECORD file, or None if the file does not exist.

Return type

Optional[List[RecordEntry]]

Returns

A dist_meta.record.RecordEntry object for each line in the record (i.e. each file in the distribution). This includes files in the *.dist-info directory.

__repr__()[source]

Returns a string representation of the DistributionType.

Return type

str

namedtuple Distribution(name, version, path)[source]

Bases: DistributionType

Represents an installed Python distribution.

Fields
  1.  name (str) – The name of the distribution.

  2.  version (Version) – The version number of the distribution.

  3.  path (PathPlus) – The path to the *.dist-info directory in the file system.

classmethod from_path(path)[source]

Construct a Distribution from a filesystem path to the *.dist-info directory.

Parameters

path (Union[str, Path, PathLike])

Return type

Distribution

read_file(filename)[source]

Read a file from the *.dist-info directory and return its content.

Parameters

filename (str)

Return type

str

has_file(filename)[source]

Returns whether the *.dist-info directory contains a file named filename.

Parameters

filename (str)

Return type

bool

get_record()[source]

Returns the parsed content of the *.dist-info/RECORD file, or None if the file does not exist.

Return type

Optional[List[RecordEntry]]

Returns

A dist_meta.record.RecordEntry object for each line in the record (i.e. each file in the distribution). This includes files in the *.dist-info directory.

namedtuple WheelDistribution(name, version, path, wheel_zip)[source]

Bases: DistributionType

Represents a Python distribution in wheel form.

Fields
  1.  name (str) – The name of the distribution.

  2.  version (Version) – The version number of the distribution.

  3.  path (PathPlus) – The path to the .whl file.

  4.  wheel_zip (ZipFile) – The opened zip file.

A WheelDistribution can be used as a contextmanager, which will close the underlying zipfile.ZipFile when exiting the with block.

classmethod from_path(path, **kwargs)[source]

Construct a WheelDistribution from a filesystem path to the .whl file.

Parameters
Return type

WheelDistribution

read_file(filename)[source]

Read a file from the *.dist-info directory and return its content.

Parameters

filename (str)

Return type

str

has_file(filename)[source]

Returns whether the *.dist-info directory contains a file named filename.

Parameters

filename (str)

Return type

bool

get_wheel()[source]

Returns the content of the *.dist-info/WHEEL file.

Raises

FileNotFoundError – if the file does not exist.

Return type

MetadataMapping

get_record()[source]

Returns the parsed content of the *.dist-info/RECORD file, or None if the file does not exist.

Return type

List[RecordEntry]

Returns

A dist_meta.record.RecordEntry object for each line in the record (i.e. each file in the distribution). This includes files in the *.dist-info directory.

Raises

FileNotFoundError – if the file does not exist.

exception DistributionNotFoundError[source]

Bases: ValueError

Raised when a distribution cannot be located.

_DT = TypeVar(_DT, bound=DistributionType)

Type:    TypeVar

Invariant TypeVar bound to dist_meta.distributions.DistributionType.