Metadata Please
metadata-please is a Python library designed for simple extraction of metadata from Python distribution artifacts. It supports extracting metadata from source distributions (sdists), wheel files, and project directories containing `pyproject.toml` or `setup.py`. The current version is 0.2.1, with releases occurring on an as-needed basis for improvements and bug fixes.
Common errors
-
FileNotFoundError: [Errno 2] No such file or directory: 'non_existent_package-1.0.0.tar.gz'
cause The path provided to `get_metadata` does not point to an existing file or directory.fixVerify that the path to your sdist, wheel, or project directory is correct and accessible. Use `pathlib.Path` to construct paths robustly. -
metadata_please.exceptions.InvalidMetadata: Could not extract metadata from provided path. No pyproject.toml, setup.py, or recognized artifact found.
cause The provided file or directory is not a recognized Python distribution artifact (sdist/wheel) and does not contain `pyproject.toml` or `setup.py` for direct source metadata extraction.fixEnsure the input path points to a valid `.tar.gz` (sdist), `.whl` (wheel), or a directory containing standard Python project configuration files (`pyproject.toml` or `setup.py`). -
AttributeError: 'DistributionMetadata' object has no attribute 'unsupported_field'
cause Attempting to access a metadata field that is not part of the standard Python core metadata or not extracted by the library. The `DistributionMetadata` object exposes common fields like `name`, `version`, `summary`, etc..fixConsult Python's Core metadata specifications or `metadata-please` documentation to understand available fields. If a field is missing, it might not be standard or could not be parsed.
Warnings
- gotcha The library primarily extracts *core metadata* as defined by Python packaging standards. While it can read `setup.py` and `pyproject.toml`, it may not extract all arbitrary fields or dynamic metadata generated by complex build systems (e.g., `setuptools_scm`).
- gotcha Direct parsing of `setup.py` (added in v0.2.0) has inherent limitations as `setup.py` is executable Python code. `metadata-please` attempts to safely extract static metadata, but complex or dynamically generated values might not be fully captured without executing the file in a controlled environment.
Install
-
pip install metadata-please
Imports
- get_metadata
from metadata_please import get_metadata
Quickstart
from metadata_please import get_metadata
from pathlib import Path
import tempfile
import shutil
# Create dummy files for demonstration
def create_dummy_sdist(path):
path.mkdir(parents=True, exist_ok=True)
with open(path / 'setup.py', 'w') as f:
f.write("from setuptools import setup; setup(name='dummy_sdist', version='0.1.0')")
# In a real scenario, you'd run `python setup.py sdist`
# For this example, we'll just point to the dir as if it were an sdist source
def create_dummy_pyproject_toml(path):
path.mkdir(parents=True, exist_ok=True)
with open(path / 'pyproject.toml', 'w') as f:
f.write('[project]\nname = "dummy-toml"\nversion = "0.2.0"')
with tempfile.TemporaryDirectory() as tmpdir:
tmp_path = Path(tmpdir)
# Example 1: Project directory with setup.py (simplified as source dir)
sdist_source_dir = tmp_path / "dummy_sdist_project"
create_dummy_sdist(sdist_source_dir)
metadata_sdist = get_metadata(sdist_source_dir)
print(f"sdist project Name: {metadata_sdist.name}, Version: {metadata_sdist.version}")
# Example 2: Project directory with pyproject.toml
pyproject_source_dir = tmp_path / "dummy_toml_project"
create_dummy_pyproject_toml(pyproject_source_dir)
metadata_toml = get_metadata(pyproject_source_dir)
print(f"pyproject.toml project Name: {metadata_toml.name}, Version: {metadata_toml.version}")
# Note: For actual .tar.gz (sdist) or .whl (wheel) files,
# you would replace the paths with your actual artifact files.
# Creating real sdist/wheel files dynamically for a quickstart is complex.
# The library is designed to work with these file types directly.
# Example (conceptual for artifact files):
# real_sdist_path = Path("path/to/your_package-1.0.0.tar.gz")
# metadata_real_sdist = get_metadata(real_sdist_path)
# print(f"Real sdist Name: {metadata_real_sdist.name}")