pkginfo
pkginfo is a Python library providing an API for querying metadata from Python package distributions. It can extract information from source distributions (sdists), binary distributions (bdists, including wheels), and installed packages by inspecting PKG-INFO, EGG-INFO, or dist-info files/directories. The library is actively maintained, with version 1.12.1.2 released in February 2025, and maintains a consistent release cadence.
Warnings
- breaking Older versions of `pkginfo` (specifically pre-1.10.0) may not correctly parse or fully support newer PEP metadata versions (e.g., Metadata-Version 2.3 and higher) used by modern Python packages, leading to errors or incomplete information.
- breaking Support for Python 2.7 was officially dropped in version 1.90. Users on Python 2.7 will encounter compatibility issues.
- gotcha The `pkginfo2` fork was created to address concerns regarding the original `pkginfo`'s historical potential for 'arbitrary imports and eval' when parsing package metadata. While `pkginfo` has evolved, users with stringent security requirements or those processing untrusted package metadata should be aware of this historical context and consider their threat model.
- gotcha When using `pkginfo.Installed` to query an installed package, the package's `.egg-info` or `.dist-info` metadata directory must be discoverable (e.g., located inside the package or adjacent to it). If metadata is not in a standard location, `pkginfo` may fail to find it.
Install
-
pip install pkginfo
Imports
- SDist
from pkginfo import SDist
- BDist
from pkginfo import BDist
- Wheel
from pkginfo import Wheel
- Installed
from pkginfo import Installed
- Develop
from pkginfo import Develop
Quickstart
import os
from pkginfo import SDist, Wheel, Installed
# Example for a source distribution (sdist)
# Assuming 'example_package-1.0.tar.gz' exists in the current directory
# For a real-world use, replace with an actual sdist path
sdist_path = 'path/to/your/package-1.0.tar.gz'
if os.path.exists(sdist_path):
sdist_info = SDist(sdist_path)
print(f"SDist Name: {sdist_info.name}, Version: {sdist_info.version}, Metadata-Version: {sdist_info.metadata_version}")
else:
print(f"SDist example skipped: {sdist_path} not found. Please provide a valid sdist file.")
# Example for a wheel distribution
# Assuming 'example_package-1.0-py3-none-any.whl' exists
wheel_path = 'path/to/your/package-1.0-py3-none-any.whl'
if os.path.exists(wheel_path):
wheel_info = Wheel(wheel_path)
print(f"Wheel Name: {wheel_info.name}, Version: {wheel_info.version}, Metadata-Version: {wheel_info.metadata_version}")
else:
print(f"Wheel example skipped: {wheel_path} not found. Please provide a valid wheel file.")
# Example for an installed package (e.g., 'pip')
try:
installed_info = Installed('pip')
print(f"Installed Name: {installed_info.name}, Version: {installed_info.version}, Metadata-Version: {installed_info.metadata_version}")
except Exception as e:
print(f"Could not get info for 'pip' (maybe not installed or metadata not found): {e}")