CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pipdeptree

Command line utility to show dependency tree of packages.

Pending
Overview
Eval results
Files

package-discovery.mddocs/

Package Discovery

The package discovery system provides functionality to find and query installed Python packages across different environments, interpreters, and installation scopes.

Capabilities

Package Discovery Function

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
    """

Usage Examples

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"]
)

Environment Detection

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:

  • venv/virtualenv: Uses VIRTUAL_ENV environment variable
  • conda: Uses CONDA_PREFIX environment variable
  • Poetry: Executes poetry env info --executable
from pipdeptree._detect_env import detect_active_interpreter

# Auto-detect current virtual environment
interpreter_path = detect_active_interpreter()
packages = get_installed_distributions(interpreter=interpreter_path)

Internal Discovery Functions

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]: ...

Exceptions

class InterpreterQueryError(Exception):
    """A problem occurred while trying to query a custom interpreter."""

Environment Variables

The discovery system respects several environment variables:

  • VIRTUAL_ENV: Path to virtual environment (venv/virtualenv)
  • CONDA_PREFIX: Path to conda environment
  • PYTHONPATH: Additional module search paths

Path Resolution

The system handles various installation locations:

  • Global site-packages: System-wide Python installations
  • Virtual environments: Isolated package environments
  • User site directory: User-specific package installations (--user)
  • Custom paths: Explicitly provided search locations

Install with Tessl CLI

npx tessl i tessl/pypi-pipdeptree

docs

cli-interface.md

data-models.md

environment-detection.md

index.md

output-rendering.md

package-discovery.md

validation.md

warning-system.md

tile.json