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.
>>> 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')>
High Level Interface#
- class packaging.metadata.Metadata#
Representation of distribution metadata.
RawMetadata, this class provides objects representing metadata fields instead of only using built-in types. Any invalid metadata will cause
InvalidMetadatato be raised (with a
__cause__attribute as appropriate).
- classmethod from_raw(data, *, validate=True)#
Create an instance from
If validate is true, all metadata will be validated. All exceptions related to validation will be gathered and raised as an
- 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
- 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]#
- dynamic: _Validator[List[str]]#
Dynamic (multiple use) (validated against core metadata field names and lowercased)
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
Metadatawhose 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)#
Parse a distribution’s metadata stored as email headers (e.g. from
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.
- class packaging.metadata.InvalidMetadata#
A metadata field contains invalid data.