modulegraph

raw JSON →
0.19.7 verified Fri May 01 auth: no python

Python module dependency analysis tool that builds a directed graph of module dependencies. Current version 0.19.7, maintained by Ronald Oussoren. Releases are irregular, primarily as needed.

pip install modulegraph
error ModuleNotFoundError: No module named 'modulegraph'
cause Modulegraph not installed in the current Python environment.
fix
Run 'pip install modulegraph' in your environment.
error AttributeError: 'ModuleGraph' object has no attribute 'find_modules'
cause Using deprecated API removed since version 0.18.
fix
Use 'run_script' instead: g.run_script('your_script.py').
error TypeError: 'NoneType' object is not subscriptable (when accessing node.filename)
cause Node may not have a filename (e.g., built-in modules). In older versions, filename could be None.
fix
Check if node.filename is not None before using it, or use getattr(node, 'filename', '').
breaking In modulegraph 0.18 and later, the internal node attributes changed. Accessing 'type' or 'filename' directly on a node may break if you relied on old values.
fix Use node.identifier and node.filename (now always a str) instead of node.type or node.filename (could be None).
deprecated The 'find_modules' method is deprecated in favor of 'run_script' and 'import_hook'.
fix Replace calls to g.find_modules('script.py') with g.run_script('script.py').
gotcha ModuleGraph does not automatically re-analyze if the script's imports change. You must create a new graph instance for a fresh analysis.
fix Always instantiate a new ModuleGraph() for each independent dependency analysis.

Analyze dependencies of a Python script.

from modulegraph import ModuleGraph
g = ModuleGraph()
g.run_script('example.py')
for node in g.nodes():
    print(node.identifier, node.filename)