Skip to main content

Interactive dependency graph & blast-radius analyzer for Python projects

Project description

depgraph ๐Ÿ”

Interactive Python dependency graph & blast-radius analyzer

Know exactly what breaks before you refactor.

PyPI version Python 3.8+ License: MIT


The Problem

You want to delete a function, rename a class, or split a module โ€” but you have no idea what else will break. Your IDE shows one level of imports. You need the full picture.

depgraph gives you that picture in seconds:

  • ๐Ÿ’ฅ Blast radius โ€” "if I delete X, these 14 things break"
  • ๐Ÿ”ด Circular import detection โ€” every cycle, with severity scores and fix suggestions
  • ๐Ÿ‘ป Orphan detection โ€” dead code that nothing imports
  • ๐ŸŒ Interactive D3.js graph โ€” zoomable, draggable, folder-clustered, with a click-to-inspect node panel

Zero execution โ€” purely static analysis via Python's built-in ast module.


Install

pip install depgraph-py

Quick Start

# Scan your project and get a full summary
depgraph scan ./my_project

# Open the interactive graph in your browser
depgraph visualize ./my_project

# Find what breaks if you remove a symbol
depgraph impact ./my_project payments.processor.PaymentProcessor

# Detect all circular imports
depgraph cycles ./my_project

# Find unused modules (dead code candidates)
depgraph orphans ./my_project

# See what a module depends on
depgraph deps ./my_project payments.processor

Using python -m (if the CLI command is blocked)

If your organisation's security policy blocks installed entry points, run every command via python -m depgraph.cli instead โ€” behaviour is identical:

python -m depgraph.cli scan     "D:\my_project"
python -m depgraph.cli visualize "D:\my_project"
python -m depgraph.cli impact   "D:\my_project" payments.processor.PaymentProcessor
python -m depgraph.cli cycles   "D:\my_project" --strict
python -m depgraph.cli orphans  "D:\my_project"
python -m depgraph.cli deps     "D:\my_project" smtp

Windows paths with spaces must be quoted: (Example)

python -m depgraph.cli deps "C:\my-project\project" module_name

Commands

depgraph scan <path>

Full project summary โ€” files, modules, classes, functions, cycle count, orphan count, and a hot-spots table of the most depended-on modules.

โ•ญโ”€ depgraph โ€” /home/user/my_project โ”€โ•ฎ
โ”‚                                     โ”‚
โ”‚  ๐Ÿ“ Python files       42           โ”‚
โ”‚  ๐Ÿ“ฆ Modules            38           โ”‚
โ”‚  ๐Ÿ›๏ธ  Classes           121           โ”‚
โ”‚  โš™๏ธ  Functions          489           โ”‚
โ”‚  ๐Ÿ”— Dependencies       734           โ”‚
โ”‚  ๐Ÿ”ด Circular imports     2           โ”‚
โ”‚  ๐Ÿ‘ป Orphan modules       5           โ”‚
โ”‚                                     โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

๐Ÿ”ฅ Most depended-on modules:
  utils.logger          โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ 16
  db.models             โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ     12
  core.config           โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ          8

depgraph impact <path> <symbol>

Show exactly what depends on a symbol โ€” directly and transitively. Tells you the full blast radius before you touch anything.

depgraph impact . payments.processor.PaymentProcessor
๐Ÿ’ฅ Blast Radius โ€” payments.processor.PaymentProcessor

Total impact: 11 nodes affected (22.9% of project)

๐Ÿ”ด Direct dependents (break immediately):
  โ†’ api.views.PaymentView
  โ†’ tests.test_payments

๐ŸŸก Transitive dependents (break indirectly):
  โ†’ api.router
  โ†’ main
  ... and 7 more

depgraph cycles <path>

Detect every circular import chain in the project. Each cycle gets a severity rating (CRITICAL / HIGH / MEDIUM / LOW) and a concrete fix suggestion.

depgraph cycles ./my_project

# Exit with code 1 if any cycles found โ€” useful in CI
depgraph cycles ./my_project --strict
๐Ÿ”„ Found 2 circular import(s)

[CRITICAL] Cycle 1 (2 nodes)
  auth.service โ†’ db.models โ†’ auth.service
  ๐Ÿ’ก Extract shared logic into a new common.py module

[HIGH] Cycle 2 (4 nodes)
  api.views โ†’ orders.handler โ†’ payments.processor โ†’ api.views
  ๐Ÿ’ก Use dependency injection or move shared types to interfaces.py

depgraph visualize <path>

Generate a self-contained interactive HTML graph and open it in your browser. No server needed โ€” it's a single file you can share with your team.

# Full project graph
depgraph visualize ./my_project

# Pre-highlight the blast radius of a symbol
depgraph visualize ./my_project --highlight payments.processor

# Save to a specific path
depgraph visualize ./my_project --output reports/deps.html

# Generate but don't open the browser
depgraph visualize ./my_project --no-open

What you get in the graph:

Feature Description
Folder cluster bubbles Dashed coloured hulls group nodes by top-level package
Click to inspect A slide-in panel shows blast radius, stats, file path, and dependency lists
Search bar Header search โ€” type to filter nodes, โ†‘โ†“ to navigate, Enter to jump
Folder filter Click a folder in the sidebar to isolate it
View modes Full Graph / Circular Imports only / Orphan Modules only
Zoom & pan Mouse wheel + drag, or toolbar buttons
Blast radius highlight Orange = direct dependents, gold = transitive

Node colour coding:

Colour Meaning
๐Ÿ”ต Blue Module (.py file)
๐ŸŸฃ Purple Class
๐Ÿฉต Teal Function
๐Ÿ”ด Red (glowing) Part of a circular import cycle
๐ŸŸก Yellow Orphan โ€” nothing imports this
๐ŸŸ  Orange Blast radius โ€” direct dependent of selected node

depgraph orphans <path>

Find modules, classes, or functions that nothing else imports. These are either dead code or entry points (like main.py or cli.py).

๐Ÿ‘ป 5 orphan(s) found (nothing imports them):

  ๐Ÿ‘ป scripts.migrate_data   (module)   โ€” scripts/migrate_data.py
  ๐Ÿ‘ป utils.old_helpers      (module)   โ€” utils/old_helpers.py
  ๐Ÿ‘ป tests.conftest         (module)   โ€” tests/conftest.py

depgraph deps <path> <symbol>

Show what a specific symbol depends on โ€” its own imports and all transitive dependencies.

depgraph deps . payments.processor
๐Ÿ”— Dependencies of payments.processor

Direct dependencies:
  โ†’ utils.logger
  โ†’ db.models

Transitive dependencies:
  โ†’ core.config
  โ†’ core.base

Python API

Use depgraph programmatically in your own scripts or tools:

from pathlib import Path
from depgraph.crawler import crawl_project
from depgraph.parser import parse_project
from depgraph.graph import build_graph
from depgraph.analyzer import Analyzer
from depgraph.visualizer import visualize

root = Path("./my_project")

# 1. Build the graph
py_files = crawl_project(str(root))
modules  = parse_project(py_files, root)
graph    = build_graph(modules)

# 2. Analyze
analyzer = Analyzer(graph)

# Blast radius of a symbol
result = analyzer.blast_radius("payments.processor.PaymentProcessor")
print(f"Affects {result.total_impact} nodes ({result.impact_percentage}%)")
print("Direct dependents:",     result.direct_dependents)
print("Transitive dependents:", result.transitive_dependents)

# Find all circular imports
for cycle in analyzer.find_cycles():
    print(f"[{cycle.severity}] {' โ†’ '.join(cycle.nodes)}")

# Find orphaned modules
print("Orphans:", analyzer.find_orphans())

# 3. Generate the interactive HTML graph
html_path = visualize(
    graph,
    output_path="my_graph.html",
    cycles=analyzer.find_cycles(),
    orphans=analyzer.find_orphans(),
    project_name="my_project",
)
print("Graph saved to:", html_path)

CI/CD Integration

Catch circular imports on every pull request:

# .github/workflows/depgraph.yml
name: Dependency Check

on: [push, pull_request]

jobs:
  check-cycles:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.11"
      - run: pip install depgraph-py
      - name: Check for circular imports
        run: depgraph cycles . --strict

How It Works

depgraph does everything through static analysis โ€” it never runs your code.

Your .py files
      โ†“
  AST Parser              โ† extracts imports, classes, functions
      โ†“                      resolves relative imports (., .., ...)
  NetworkX DiGraph        โ† directed graph: A โ†’ B means A imports B
      โ†“
  Impact Analyzer         โ† reverse BFS to compute blast radius
  Cycle Detector          โ† Tarjan's SCC algorithm
  Orphan Finder           โ† nodes with zero incoming edges
      โ†“
  D3.js HTML Graph        โ† self-contained interactive visualization

Project Structure

depgraph/
โ”œโ”€โ”€ crawler.py       โ€” walks .py files, skips venv / __pycache__
โ”œโ”€โ”€ parser.py        โ€” AST extraction, resolves relative imports
โ”œโ”€โ”€ graph.py         โ€” builds the NetworkX directed graph
โ”œโ”€โ”€ analyzer.py      โ€” blast radius, cycle detection, orphan finding
โ”œโ”€โ”€ visualizer.py    โ€” generates the D3.js interactive HTML
โ””โ”€โ”€ cli.py           โ€” Typer CLI (6 commands)

Requirements

  • Python 3.8+
  • networkx >= 3.0 โ€” graph engine
  • typer >= 0.9.0 โ€” CLI framework
  • rich >= 13.0.0 โ€” terminal output

The visualizer uses D3.js 7 via CDN โ€” no extra Python dependencies needed. A corporate-friendly CDN fallback is included automatically.


Development

git clone https://github.com/yourusername/depgraph
cd depgraph
pip install -e .
pytest tests/ -v

License

MIT โ€” free for personal and commercial use.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

depgraph_py-0.1.0.tar.gz (213.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

depgraph_py-0.1.0-py3-none-any.whl (30.2 kB view details)

Uploaded Python 3

File details

Details for the file depgraph_py-0.1.0.tar.gz.

File metadata

  • Download URL: depgraph_py-0.1.0.tar.gz
  • Upload date:
  • Size: 213.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for depgraph_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 233fd910fd1a5f31332b341f4e28612cde2c1706add3310261f8efeea0faac72
MD5 3dd37da3d870bc4d72933f3e34e2638c
BLAKE2b-256 ce69a736e39e99e552ef3e0ebd6b27f3d819c37bd76ccabc11c001b85b062f59

See more details on using hashes here.

File details

Details for the file depgraph_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: depgraph_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 30.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for depgraph_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d0e1c3240fd15f8d500d426f6199c9d18c9066dd24e54947c045813dac4d1b48
MD5 4164dc81d3f5662622fa202cc04922e8
BLAKE2b-256 614b5f24f8a8f84216a8df90458b18c9bfc1cc3a7ce38f037f3ef6b651926d39

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page