Command line utility to show dependency tree of packages.
—
The package discovery system provides functionality to find and query installed Python packages across different environments, interpreters, and installation scopes.
The main entry point for discovering installed Python packages in various environments.
def get_installed_distributions(
interpreter: str = sys.executable or "",
supplied_paths: list[str] | None = None,
local_only: bool = False,
user_only: bool = False,
) -> list[Distribution]:
"""
Return the distributions installed in the interpreter's environment.
Parameters:
- interpreter: Python interpreter to inspect (default: current executable)
- supplied_paths: Custom paths to restrict package search locations
- local_only: Only show packages in virtual environment (if in venv)
- user_only: Only show packages in user site directory
Returns:
List of importlib.metadata.Distribution objects
Raises:
InterpreterQueryError: If failure occurred while querying interpreter
"""import sys
from pipdeptree._discovery import get_installed_distributions
# Get all packages from current interpreter
packages = get_installed_distributions()
# Get packages from specific interpreter
packages = get_installed_distributions(interpreter="/path/to/python")
# Get only local virtualenv packages
packages = get_installed_distributions(local_only=True)
# Get only user-installed packages
packages = get_installed_distributions(user_only=True)
# Use custom paths to restrict search
packages = get_installed_distributions(
supplied_paths=["/custom/site-packages"]
)Automatic detection of virtual environments including venv, virtualenv, conda, and Poetry.
def detect_active_interpreter() -> str:
"""
Attempt to detect a venv, virtualenv, poetry, or conda environment.
Returns:
Path to the detected interpreter
Raises:
SystemExit: If unable to detect virtual environment
"""Detection methods include:
VIRTUAL_ENV environment variableCONDA_PREFIX environment variablepoetry env info --executablefrom pipdeptree._detect_env import detect_active_interpreter
# Auto-detect current virtual environment
interpreter_path = detect_active_interpreter()
packages = get_installed_distributions(interpreter=interpreter_path)Additional internal functions for path resolution and filtering:
def query_interpreter_for_paths(
interpreter: str,
local_only: bool = False
) -> list[str]: ...
def filter_valid_distributions(
distributions: Iterable[Distribution]
) -> list[Distribution]: ...class InterpreterQueryError(Exception):
"""A problem occurred while trying to query a custom interpreter."""The discovery system respects several environment variables:
The system handles various installation locations:
--user)Install with Tessl CLI
npx tessl i tessl/pypi-pipdeptree