Parse and Create Python Distribution Metadata
dist-meta is a Python library (current version 0.9.0) designed to parse and create Python distribution metadata, primarily interacting with `dist-info/METADATA` files. It provides tools for reading distribution information, entry points, and `RECORD` files. Releases are made irregularly as features are added or issues resolved.
Common errors
-
dist_meta.metadata.MissingFieldError: 'Name'
cause The input metadata string or file is missing a required field, such as 'Name' or 'Version', which are mandatory in Python distribution metadata.fixEnsure all mandatory fields (e.g., `Metadata-Version`, `Name`, `Version`) are present and correctly formatted in the metadata string or file being parsed. -
AttributeError: 'str' object has no attribute 'read'
cause Attempted to pass a string containing metadata directly to `dist_meta.metadata.load()`, which expects a file-like object with a `read()` method.fixUse `from dist_meta.metadata import loads` and pass the metadata as a string to the `loads()` function. Alternatively, if you intend to read from a file, open the file and pass the file object to `load()`.
Warnings
- gotcha When parsing metadata, ensure the input string or file adheres to the Python Core Metadata Specifications (e.g., PEP 643 for Metadata-Version 2.2+ or earlier PEPs). Incorrectly formatted or incomplete metadata may lead to parsing errors or missing fields.
- gotcha The `loads()` function is for parsing a string, while `load()` is for parsing a file-like object. Using a file path directly with `loads()` or string content with `load()` will result in `TypeError` or `AttributeError`.
Install
-
pip install dist-meta
Imports
- loads
from dist_meta.metadata import loads
- get_distribution
from dist_meta.distributions import get_distribution
- EntryPoint
from dist_meta.entry_points import EntryPoint
Quickstart
from dist_meta.metadata import loads
metadata_content = '''\
Metadata-Version: 2.1
Name: my-example-package
Version: 1.2.3
Summary: A simple example Python package.
Author: Example Developer
Author-Email: dev@example.com
Requires-Python: >=3.6.1
''')
parsed_metadata = loads(metadata_content)
print(f"Package Name: {parsed_metadata['Name']}")
print(f"Package Version: {parsed_metadata['Version']}")
print(f"Requires Python: {parsed_metadata['Requires-Python']}")