pip-api: Unofficial Importable pip API
pip-api is an unofficial Python library providing a programmatic interface to pip's functionality. It allows developers to inspect installed packages, run pip commands, and manage package metadata directly from Python code. The current version is 0.0.34. Due to its nature of wrapping pip's internal APIs, its release cadence is closely tied to pip's updates and internal changes.
Warnings
- breaking Support for Python 3.7 was dropped in version 0.0.34. Users on Python 3.7 or older will need to upgrade their Python version or use an older `pip-api` release.
- gotcha `pip-api` is an *unofficial* API and relies on `pip`'s internal structure, which is not guaranteed to be stable. Updates to `pip` itself can introduce breaking changes for `pip-api` without warning, potentially requiring `pip-api` updates or causing unexpected behavior.
- gotcha The `pip_api.run()` function directly executes pip commands, which can have side effects such as installing, uninstalling, or upgrading packages in the current environment. This is not a sandboxed operation.
- gotcha The `pip_api.installed_packages()` function might incur performance overhead due to parsing package metadata. While `pip-api` aims to be efficient, repeated calls in performance-critical loops might be slow.
Install
-
pip install pip-api
Imports
- installed_packages
from pip_api import installed_packages
- run
from pip_api import run
Quickstart
from pip_api import installed_packages, run
# List all installed packages
print("Installed Packages:")
for package_name, package_info in installed_packages().items():
print(f" {package_info.name}=={package_info.version}")
# Example of running a pip command (e.g., showing details of a package)
# Note: This executes pip directly and can have side effects.
# Use a package that's likely installed, like 'pip' itself.
try:
# 'run' returns a (stdout, stderr) tuple
stdout, stderr = run(['show', 'pip'])
print("\n'pip show pip' output:\n" + stdout.decode('utf-8'))
except Exception as e:
print(f"Error running pip command: {e}")