Map every import dependency in a Python project and catch circular imports — using only Python's standard library.
py-import-graph performs AST-based static analysis on your entire Python project, building a complete directed dependency graph and running cycle detection to surface circular imports before they crash your app at runtime.
- Circular imports cause
ImportErroror subtle attribute errors that are hard to debug - Large codebases accumulate tangled imports over time without anyone noticing
- No existing stdlib tool gives you a project-wide dependency map and cycle detector in one command
- 🌐 Project-wide analysis — walks all
.pyfiles recursively - 🔁 Cycle detection — DFS-based, finds all circular import chains
- 🌳 ASCII dependency tree — visual tree of who imports what
- 📊 Most-imported ranking — shows your most-depended-on modules
- 🎨 Graphviz DOT export — render with
dot -Tsvg graph.dot > graph.svg - 🤖 JSON output — pipe into CI scripts or dashboards
- 🏃 Zero dependencies — pure Python stdlib
git clone https://github.com/sushii15/py-import-graph
cd py-import-graph
# No pip install needed!Requires Python 3.10+.
python import_graph.py <project_path> [options]| Flag | Description |
|---|---|
--dot |
Output Graphviz DOT format |
--show-all |
Include stdlib & third-party imports |
--cycles-only |
Print only circular import chains |
--json |
Machine-readable JSON summary |
# Full report for current directory
python import_graph.py .
# Check for circular imports in a project
python import_graph.py /path/to/project --cycles-only
# Export to Graphviz (requires graphviz installed separately)
python import_graph.py . --dot > graph.dot
dot -Tsvg graph.dot > graph.svg
# JSON output for CI
python import_graph.py . --json | jq '.circular_imports'🔍 py-import-graph · myproject
══════════════════════════════════════════════════════
Modules analysed : 12
Import edges : 18
Most-imported modules:
5× utils
3× models
2× config
⚠️ Circular imports : 1
[1] auth → session → auth
📊 Dependency Tree:
📦 app
├── models
│ └── utils
└── auth
└── session ♻
📦 cli
└── config
- Recursively scans
*.pyfiles (skipping.venv,__pycache__, etc.) - Parses each file with Python's
astmodule to extractimportandfrom ... importstatements - Resolves relative imports (
.models,..utils) to absolute module names - Builds a directed graph:
module → {set of imported modules} - Filters to project-internal modules by default (no stdlib noise)
- Runs depth-first search to enumerate all strongly-connected components (cycles)
python test_import_graph.py -vTests use only tempfile and stdlib — no fixtures or extra setup required.
- Fork the repo
- Create a feature branch:
git checkout -b feat/your-feature - Commit:
git commit -m 'feat: add your feature' - Push and open a PR
MIT License — see LICENSE for details.