pip-licenses-lib

raw JSON →
1.1.0 verified Mon Apr 27 auth: no python

A library to retrieve the software license list of Python packages installed with pip. Provides programmatic access to license metadata, with support for PEP 639, PEP 770, and SBOM files. Current version 1.1.0, requires Python >=3.10, released approximately every few months.

pip install pip-licenses-lib
error AttributeError: 'Package' object has no attribute 'name'
cause Using old dict-style access on a Package dataclass after upgrading from <0.3.0.
fix
Update code to use attribute access: pkg.name, pkg.version, etc.
error ImportError: cannot import name 'Package' from 'pip_licenses_lib'
cause Attempting to import Package from top-level package instead of the models submodule.
fix
Use 'from pip_licenses_lib.models import Package' instead.
error TypeError: 'Package' object is not subscriptable
cause Trying to use square brackets on a Package dataclass instance (e.g., pkg['name']).
fix
Access fields using dot notation: pkg.name, pkg.version, etc.
breaking In v0.3.0, the return type changed from dict to dataclass (Package). Attribute names were renamed to match PEP8. Code accessing dict keys or old attribute names will break.
fix Upgrade to >=0.3.0 and use object attributes (e.g., pkg.name, pkg.version) instead of dictionary subscripting.
gotcha Package model is not importable from the top-level; must import from pip_licenses_lib.models.Package. Doing from pip_licenses_lib import Package raises ImportError.
fix Use from pip_licenses_lib.models import Package.
deprecated Python <=3.9 support dropped in v0.5.0; Python <=3.9 dropped again in v1.0.0 (now requires >=3.10).
fix Upgrade Python to >=3.10 for latest version.

Retrieve license info for all installed packages and print first 5 (name, license expression, version).

from pip_licenses_lib import get_packages

packages = get_packages()
for pkg in packages[:5]:
    print(pkg.name, pkg.license_expression, pkg.version)