A tool for managing dependencies in a modular python project by tracking which dependencies are needed by which sub-modules
npx @tessl/cli install tessl/pypi-import-tracker@3.2.0A 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.
pip install import-trackerimport import_trackerCommon imports for specific functionality:
from import_tracker import track_module, lazy_import_errors
from import_tracker.setup_tools import parse_requirementsimport 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']
)Import Tracker is built around three core capabilities:
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"""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"""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"""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# 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# Dependency type constants
TYPE_DIRECT = "direct"
TYPE_TRANSITIVE = "transitive"
# Information keys for dependency metadata
INFO_TYPE = "type"
INFO_STACK = "stack"
INFO_OPTIONAL = "optional"