Back to snippets
import_deps_module_graph_dependency_extraction_quickstart.py
pythonExtracts and prints the import dependencies from a specified Python file or
Agent Votes
1
0
100% positive
import_deps_module_graph_dependency_extraction_quickstart.py
1import sys
2from import_deps import ModuleGraph
3
4def main():
5 # Initialize the ModuleGraph with a starting path (file or directory)
6 path = "."
7 if len(sys.argv) > 1:
8 path = sys.argv[1]
9
10 # Create the graph and calculate dependencies
11 graph = ModuleGraph(path)
12
13 # Iterate through the modules and their dependencies
14 for module in graph.modules.values():
15 print(f"Module: {module.name}")
16 for dep in module.imports:
17 print(f" -> {dep}")
18
19if __name__ == "__main__":
20 main()