or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

dependency-tracking.mdindex.mdlazy-import-errors.mdsetup-tools.md
tile.json

tessl/pypi-import-tracker

A tool for managing dependencies in a modular python project by tracking which dependencies are needed by which sub-modules

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/import-tracker@3.2.x

To install, run

npx @tessl/cli install tessl/pypi-import-tracker@3.2.0

index.mddocs/

Import Tracker

A comprehensive Python library for managing dependencies in modular Python projects. Import Tracker provides tools for tracking module dependencies, implementing lazy import error handling to prevent crashes from uninstalled optional dependencies, and programmatically determining setuptools requirements.

Package Information

  • Package Name: import-tracker
  • Language: Python
  • Installation: pip install import-tracker

Core Imports

import import_tracker

Common imports for specific functionality:

from import_tracker import track_module, lazy_import_errors
from import_tracker.setup_tools import parse_requirements

Basic Usage

import import_tracker

# Track dependencies of a module
deps = import_tracker.track_module('my_module')
print(deps)  # {'my_module': ['requests', 'numpy']}

# Enable lazy import errors globally
from import_tracker import lazy_import_errors
lazy_import_errors()

# Use as context manager for specific imports
with lazy_import_errors():
    from . import optional_module  # Won't crash if missing

# Parse requirements for setuptools
from import_tracker.setup_tools import parse_requirements
install_requires, extras_require = parse_requirements(
    requirements='requirements.txt',
    library_name='my_package',
    extras_modules=['my_package.widgets']
)

Architecture

Import Tracker is built around three core capabilities:

  • Dependency Tracking: Uses bytecode analysis to discover all module dependencies, including direct, transitive, and optional dependencies
  • Lazy Import Errors: Implements a custom import system using meta path finders and loaders to defer ImportError exceptions until actual usage
  • Setup Tools Integration: Bridges dependency analysis with setuptools configuration by automatically generating install_requires and extras_require

Capabilities

Dependency Tracking

Comprehensive module dependency analysis with support for submodule tracking, import stack tracing, and direct vs transitive dependency classification.

def track_module(
    module_name: str,
    package_name: Optional[str] = None,
    submodules: Union[List[str], bool] = False,
    track_import_stack: bool = False,
    full_depth: bool = False,
    detect_transitive: bool = False,
    show_optional: bool = False,
) -> Union[Dict[str, List[str]], Dict[str, Dict[str, Any]]]:
    """Track the dependencies of a single python module"""

Dependency Tracking

Lazy Import Error Handling

Context manager and function for deferring ImportError exceptions until modules are actually used, with support for custom error messages and extras integration.

def lazy_import_errors(
    *,
    get_extras_modules: Optional[Callable[[], Set[str]]] = None,
    make_error_message: Optional[Callable[[str], str]] = None,
):
    """Enable lazy import errors"""

Lazy Import Errors

Setup Tools Integration

Automatic generation of setuptools configuration from dependency analysis, supporting install_requires and extras_require computation.

def parse_requirements(
    requirements: Union[List[str], str],
    library_name: str,
    extras_modules: Optional[List[str]] = None,
    full_depth: bool = True,
    keep_optional: Union[bool, Dict[str, List[str]]] = False,
    **kwargs,
) -> Tuple[List[str], Dict[str, List[str]]]:
    """Parse requirements and generate install_requires and extras_require"""

Setup Tools Integration

Command Line Interface

Import Tracker can be run as a command-line tool for dependency analysis:

# Track a single module
python -m import_tracker --name my_library

# Track with all submodules and show optional dependencies
python -m import_tracker --name my_library --submodules --show_optional

# Generate detailed analysis with import stacks
python -m import_tracker --name my_library --track_import_stack --detect_transitive --indent 2

Types

# Type aliases used throughout the package
Union[Dict[str, List[str]], Dict[str, Dict[str, Any]]]  # track_module return type
Tuple[List[str], Dict[str, List[str]]]  # parse_requirements return type
Optional[Callable[[], Set[str]]]  # get_extras_modules function type
Optional[Callable[[str], str]]  # make_error_message function type

Constants

# Dependency type constants
TYPE_DIRECT = "direct"
TYPE_TRANSITIVE = "transitive"

# Information keys for dependency metadata
INFO_TYPE = "type"
INFO_STACK = "stack" 
INFO_OPTIONAL = "optional"