Python Dependency Graph Calculator

0.8.1 · abandoned · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `importlab.graph.build_graph` to create a dependency graph from a list of Python file paths. It creates two dummy modules, analyzes their imports, and prints the nodes and edges of the resulting graph. The graph object returned is a networkx.DiGraph.

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')

view raw JSON →