DepCycle is a command-line tool to visualize Python project dependencies. It helps developers understand complex codebases by automatically generating visual maps of how modules are connected, making it easy to spot architectural problems like circular dependencies and untangle coupled code.
- Automatic Dependency Discovery: Scans Python projects and builds a complete dependency graph
- Cycle Detection: Identifies circular dependencies that can lead to architectural issues
- Flexible Visualization: Multiple output formats including PNG, SVG, and HTML
- Smart Filtering: Exclude specific patterns, third-party libraries, or standard library modules
- AST-Based Parsing: Uses Python's Abstract Syntax Tree for accurate import detection
- Repository: https://github.com/Matricess/depcycle.git
- Tests: https://github.com/Matricess/depcycle/tree/main/tests
- Design commentary: https://github.com/Matricess/depcycle/blob/main/DESIGN.md
- Sample dependency graph:
examples/(local)
- Python 3.8 or higher
- Graphviz (for PNG/SVG output)
macOS:
brew install graphvizUbuntu/Debian:
sudo apt-get install graphvizWindows: Download and install from Graphviz website
pip install depcycle
or
# install directly from GitHub
pip install git+https://github.com/Matricess/depcycle.gitgit clone https://github.com/Matricess/depcycle.git
cd depcycle
pip install -e .[dev]If you prefer requirements files, pip install -r requirements.txt will install runtime deps plus pytest for the test suite.
Analyze a Python project and generate a dependency graph (PNG by default):
depcycle /path/to/your/projectThe output is written to dependencies.png in the current working directory.
Note: By default DepCycle skips common noise directories such as
venv/,.venv/,.git/,__pycache__/,node_modules/, build artifacts, and Python cache folders. Use-eflags if you need extra exclusions, or disable the defaults via the API (Project.get_python_files(include_defaults=False)).
python -m depcycle /path/to/your/projectGenerate a different output format or explicit location:
depcycle /path/to/project --format svg --output diagrams/dependencies.svgExclude specific directories or files (glob syntax):
depcycle /path/to/project -e venv -e ".*/tests/*" -e "*.test.py"Focus only on local code:
depcycle /path/to/project --no-third-party --no-stdlibFull help:
depcycle --helpTests live under tests/ and run without touching the sample projects in examples/.
pip install -e .[dev]
pytest -qSee tests/README.md for a quick summary.
depcycle/
├── src/
│ └── depcycle/
│ ├── __init__.py
│ ├── __main__.py
│ ├── cli.py # Command-line interface
│ ├── config.py # Configuration management
│ ├── graph/
│ │ ├── __init__.py
│ │ ├── dependency_graph.py # Core graph logic
│ │ └── module_node.py # Module representation
│ ├── parsing/
│ │ ├── __init__.py
│ │ ├── ast_parser.py # AST-based import parsing
│ │ └── project.py # File discovery
│ └── rendering/
│ ├── __init__.py
│ ├── interface.py # Visualization interface
│ └── visualizers.py # Output implementations
├── requirements.txt
├── README.md
└── LICENSE
DepCycle follows a clean, modular architecture:
- CLI Layer (
cli.py): Handles user input and orchestrates the workflow - Configuration (
config.py): Manages all settings and options - Graph Layer (
graph/): Core data structures for the dependency graph - Parsing Layer (
parsing/): Discovers files and extracts imports using AST - Rendering Layer (
rendering/): Generates visualizations in various formats
DepCycleCLI: Main entry point that handles command-line argumentsDependencyGraph: Central data structure holding all module relationshipsModuleNode: Represents a single Python module/fileProject: Discovers and manages Python files in a projectASTParser: Extracts imports using Python's AST moduleGraphvizVisualizer: Renders graphs as PNG/SVG imagesHtmlVisualizer: Generates interactive HTML visualizations
- Discovery: Recursively scans the project directory for all
.pyfiles - Parsing: Uses Python's AST to extract import statements from each file
- Resolution: Maps import strings to actual modules in the project
- Classification: Categorizes modules as LOCAL, THIRD_PARTY, or STDLIB
- Analysis: Detects circular dependencies using depth-first search
- Visualization: Renders the graph using Graphviz or HTML
When you run DepCycle, you'll see output like:
Analyzing project: /path/to/my-project
Building dependency graph...
Found 42 modules
✓ No circular dependencies detected
Generating PNG visualization...
✓ Visualization saved to: dependencies.png
If circular dependencies are found:
⚠️ Warning: Found 2 circular dependency cycles!
Cycle 1: app.models.user → app.services.auth → app.models.user
Cycle 2: app.core.database → app.core.config → app.core.database
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
Built as part of a Software Design and Testing course project (IT643).