pip-licenses
pip-licenses is a command-line interface (CLI) tool designed to efficiently list the software licenses of Python packages installed via pip. It is actively maintained with regular updates, aligning with the release cadence of `pip` itself. The current version is 5.5.5.
Warnings
- breaking `pip-licenses` versions 4.x and 5.x have dropped support for older Python versions. Specifically, 4.x discontinued support for Python 3.7, and versions 5.x require Python >=3.9 (dropping 3.8 support). Users on older Python environments should use an earlier `pip-licenses` version (e.g., `<4.0` for Python 3.7).
- breaking When upgrading from `pip-licenses` 3.x to 4.x or later, the `PTable` dependency was removed. You must explicitly uninstall `PTable` to avoid conflicts.
- breaking In version 3.5.0, the separator for displaying multiple licenses for a single package changed from a comma to a semicolon. This can affect scripts parsing the output.
- gotcha By default, `pip-licenses` ignores system-level packages (like `pip`, `setuptools`, and its own dependencies) to focus on user-installed project dependencies.
- gotcha `pip-licenses` is primarily a CLI tool and does not offer a direct programmatic API for import. If you are looking for a library for programmatic license detection, consider `pip-licenses-lib`, which is a separate fork providing library functionality.
- breaking Support for PEP 639 (SPDX License Expressions) was added, and with `pip-licenses 5.5.0`, the `--from=all` output now includes the `License-Expression` value. This change affects the structure of the output, particularly when using `--from=all`.
Install
-
pip install pip-licenses -
pip uninstall -y PTable
Quickstart
import os
# Install pip-licenses and a sample package (if not already installed)
# os.system('pip install Django pip-licenses')
# Basic usage: list all installed package licenses
print("\n--- Basic License List ---")
os.system('pip-licenses')
# Include system packages like pip and setuptools
print("\n--- With System Packages ---")
os.system('pip-licenses --with-system')
# Fail if a specific license (e.g., AGPL) is found
print("\n--- Fail on AGPL (example, might exit with error) ---")
# For demonstration, we use a common permissive license (MIT) to show failure.
# In a real scenario, you'd check for restrictive licenses like AGPL.
# This command will exit with a non-zero code if MIT is found.
# os.system('pip-licenses --fail-on "MIT License" --partial-match')
# Output in JSON format
print("\n--- Output in JSON Format (first 1000 chars) ---")
json_output = os.popen('pip-licenses --format=json').read()
print(json_output[:1000])
# Get a summary of licenses by count
print("\n--- License Summary ---")
os.system('pip-licenses --summary')