Python Dependency Graph Calculator
Importlab is a Python library that automatically infers dependencies and calculates a dependency graph for Python files. It can perform dependency ordering of a set of files, including cycle detection. Its primary use case is to work with static analysis tools that process one file at a time, ensuring dependencies are analyzed first. The project is currently at version 0.8.1 and its GitHub repository was archived on May 6, 2025, making it read-only.
Warnings
- breaking The `importlab` GitHub repository was archived on May 6, 2025, and is now read-only. This indicates that the library is no longer actively maintained, and no further updates, bug fixes, or new features are expected.
- gotcha `importlab` might execute module initialization code when attempting to find dependencies, which could lead to unexpected side effects or security vulnerabilities if analyzing untrusted or malformed code.
- breaking Support for Python 2 was officially dropped in version 0.6.1. Using `importlab` with Python 2 will result in errors.
- gotcha While `importlab` infers dependencies, the resulting graph object is a `networkx.DiGraph`. Users needing to traverse, query, or manipulate this graph extensively will likely require familiarity with the `networkx` library's API.
Install
-
pip install importlab
Imports
- build_graph
from importlab.graph import build_graph
Quickstart
import os
from importlab.graph import build_graph
# Create dummy Python files for demonstration
with open('module_a.py', 'w') as f:
f.write('import module_b\ndef func_a(): pass')
with open('module_b.py', 'w') as f:
f.write('def func_b(): pass')
# Build the dependency graph
# Assuming current directory is in PYTHONPATH for this example
graph = build_graph(paths=['module_a.py', 'module_b.py'])
print('Nodes (files):', graph.nodes())
print('Edges (dependencies):', graph.edges())
# Clean up dummy files
os.remove('module_a.py')
os.remove('module_b.py')