Metadata#
Both source distributions and binary distributions (_sdists_ and _wheels_, respectively) contain files recording the core metadata for the distribution. This information is used for everything from recording the name of the distribution to the installation dependencies.
Usage#
>>> from packaging.metadata import parse_email
>>> metadata = "Metadata-Version: 2.3\nName: packaging\nVersion: 24.0"
>>> raw, unparsed = parse_email(metadata)
>>> raw["metadata_version"]
'2.3'
>>> raw["name"]
'packaging'
>>> raw["version"]
'24.0'
>>> from packaging.metadata import Metadata
>>> parsed = Metadata.from_raw(raw)
>>> parsed.name
'packaging'
>>> parsed.version
<Version('24.0')>
Reference#
High Level Interface#
- class packaging.metadata.Metadata#
Representation of distribution metadata.
Compared to
RawMetadata
, this class provides objects representing metadata fields instead of only using built-in types. Any invalid metadata will causeInvalidMetadata
to be raised (with a__cause__
attribute as appropriate).- classmethod from_raw(data, *, validate=True)#
Create an instance from
RawMetadata
.If validate is true, all metadata will be validated. All exceptions related to validation will be gathered and raised as an
ExceptionGroup
.- Parameters:
data (RawMetadata) –
validate (bool) –
- Return type:
- classmethod from_email(data, *, validate=True)#
Parse metadata from email headers.
If validate is true, the metadata will be validated. All exceptions related to validation will be gathered and raised as an
ExceptionGroup
.
- metadata_version: _Validator[Literal['1.0', '1.1', '1.2', '2.1', '2.2', '2.3']]#
Metadata-Version (required; validated to be a valid metadata version)
- name: _Validator[str]#
Name (required; validated using
canonicalize_name()
and its validate parameter)
- dynamic: _Validator[List[str]]#
Dynamic (multiple use) (validated against core metadata field names and lowercased)
- description_content_type: _Validator[str]#
Description-Content-Type (validated)
- requires_dist: _Validator[List[Requirement]]#
- requires_python: _Validator[SpecifierSet]#
- provides_extra: _Validator[List[NormalizedName]]#
Low Level Interface#
- class packaging.metadata.RawMetadata#
A dictionary of raw core metadata.
Each field in core metadata maps to a key of this dictionary (when data is provided). The key is lower-case and underscores are used instead of dashes compared to the equivalent core metadata field. Any core metadata field that can be specified multiple times or can hold multiple values in a single field have a key with a plural name. See
Metadata
whose attributes match the keys of this dictionary.Core metadata fields that can be specified multiple times are stored as a list or dict depending on which is appropriate for the field. Any fields which hold multiple values in a single field are stored as a list.
- static __new__(cls, /, *args, **kwargs)#
- packaging.metadata.parse_email(data)#
Parse a distribution’s metadata stored as email headers (e.g. from
METADATA
).This function returns a two-item tuple of dicts. The first dict is of recognized fields from the core metadata specification. Fields that can be parsed and translated into Python’s built-in types are converted appropriately. All other fields are left as-is. Fields that are allowed to appear multiple times are stored as lists.
The second dict contains all other fields from the metadata. This includes any unrecognized fields. It also includes any fields which are expected to be parsed into a built-in type but were not formatted appropriately. Finally, any fields that are expected to appear only once but are repeated are included in this dict.
Exceptions#
- class packaging.metadata.InvalidMetadata#
A metadata field contains invalid data.
- class packaging.metadata.ExceptionGroup#
A minimal implementation of
ExceptionGroup
from Python 3.11.If
ExceptionGroup
is already defined by Python itself, that version is used instead.