depagon
depagon is a Python CLI tool and library that analyzes the import dependency structure of a Python project. It parses every .py file using the standard-library ast module, builds a directed graph of internal module dependencies, and reports:
- Circular imports — cycles in the dependency graph.
- Unused imports — names imported but never referenced in the same file.
- Coupling metrics — fan-in (how many modules import a given module) and fan-out (how many modules it imports).
It works as both a command-line tool (depagon scan ...) and an importable library.
Install
pip install depagon
Requires Python 3.10 or later. The only runtime dependency is rich.
Usage
Tree output (default)
Renders a dependency tree, coupling table, and findings in the terminal:
depagon scan path/to/myproject
Mermaid diagram
depagon scan path/to/myproject --output mermaid
Outputs a graph TD Mermaid string that can be pasted into any Mermaid-compatible renderer (GitHub Markdown, Mermaid Live Editor, etc.).
Graphviz DOT
depagon scan path/to/myproject --output dot
Outputs a DOT string suitable for dot -Tpng -o graph.png.
Show unused imports
depagon scan path/to/myproject --unused
Unused-import findings are collected during every run but only displayed when --unused is passed.
CI integration — exit code on cycles
depagon scan path/to/myproject --detect-cycles
Exits with status 1 if any circular imports are found, 0 otherwise. Wire it into your CI pipeline:
# GitHub Actions example
- run: depagon scan src/ --detect-cycles
Library usage
from depagon import Analyzer
from depagon.renderers import render_mermaid
result = Analyzer().analyze(Path("src/"))
# Inspect findings
for finding in result.findings:
print(finding.kind, finding.location, finding.detail)
# Render
print(render_mermaid(result))
Public API:
| Symbol | Module |
|---|---|
Scanner, ImportInfo, ModuleFile |
depagon.scanner |
DependencyGraph |
depagon.graph |
Analyzer, AnalysisResult, Finding |
depagon.analyzer |
render_tree, render_mermaid, render_dot |
depagon.renderers |
All of the above are also re-exported from depagon directly.
Skipped directories
The scanner automatically skips the following directory names to avoid scanning virtual environments and build artifacts:
__pycache__ venv .venv env .env envs virtualenv .virtualenv
node_modules .tox .mypy_cache dist build .pytest_cache .eggs site-packages
Any directory whose name starts with . (hidden directories) is also skipped.
If your environment uses a different name (e.g. my_env, .project_venv, py310), you can exclude it by subclassing Scanner and overriding _SKIP_DIRS:
from depagon.scanner import Scanner, _SKIP_DIRS
class MyScanner(Scanner):
pass
# Add your custom env name to the skip set
import depagon.scanner as _s
_s._SKIP_DIRS = _SKIP_DIRS | {"my_env", "py310", ".project_venv"}
Or build the graph programmatically and pass a custom scanner to the analyzer:
from pathlib import Path
import depagon.scanner as scanner_mod
from depagon.scanner import Scanner, _SKIP_DIRS
from depagon.analyzer import Analyzer
# Extend the skip set before scanning
scanner_mod._SKIP_DIRS = _SKIP_DIRS | {"my_env"}
result = Analyzer().analyze(Path("src/"))
How it works
-
Scanning —
Scanner.scan(root)walks the directory tree (skipping the directories listed above), reads each.pyfile, and parses it withast.parse. Files with syntax errors are skipped with a warning. Everyimportandfrom ... importstatement is recorded as anImportInfo. -
Graph construction —
Analyzer.analyze(root)resolves each import to its absolute dotted module name, checks whether it refers to a file inside the scanned directory (internal module), and adds a directed edge to aDependencyGraph. -
Cycle detection —
DependencyGraph.find_cycles()uses a colored DFS (white / gray / black) with a recursion stack. When a gray (in-progress) node is encountered again, the slice of the current path forms a cycle. Duplicate cycles are deduplicated via a frozenset key. -
Unused-import detection — For each file,
ast.walkcollects everyast.Nameidentifier. Any bound import name absent from this set is flagged. This is a conservative heuristic:import osis considered used ifosappears anywhere as an identifier in the file. -
Rendering — Three renderers produce different output formats from the same
AnalysisResult.
Building and publishing
Build a distribution:
pip install build
python -m build
This creates dist/depagon-0.1.0-py3-none-any.whl and a source tarball.
Upload to PyPI (requires a PyPI account and twine):
pip install twine
twine upload dist/*
Running tests
pip install pytest
pytest tests/ -v
Metadata
Release files for depagon 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| depagon-0.1.2.tar.gz | 132.5 kB | Without provenance | Details for depagon-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| depagon-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for depagon-0.1.2-py3-none-any.whl |
Total release size: 152.9 kB
Release files / depagon-0.1.2.tar.gz
| Download URL | depagon-0.1.2.tar.gz |
|---|---|
| Size | 132.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c74c0c37e365a1a54a3369815dc208ab48940d6fff4d978811e974b8f24ed845
|
|
BLAKE2b-256 checksum How to use checksums |
34b17a0b82d70a8f1edc321845569cf0adbe767469e45cddc61eb946e8dbce27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / depagon-0.1.2-py3-none-any.whl
| Download URL | depagon-0.1.2-py3-none-any.whl |
|---|---|
| Size | 20.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a521e5b4e49a11a15069a5593f2f08fa361a921c9fbf541da1a2594b3357cbaf
|
|
BLAKE2b-256 checksum How to use checksums |
20c35b0fe2d1bba860368f04f26ed9ec4836b89cecf5d9d56e0599d34f7b808f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Security for depagon 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.