← Back to Blog

How to Generate a Python Dependency Graph with pydeps and Graphviz

ArchToCode Team October 22, 2026 7 min read
Python dependency graphpydepsGraphvizcode visualizationPython project structuredependency analysisArchToCodecode architecturesoftware visualizationPython development
Table of contents

Understanding Your Python Project's Architecture

Python projects, especially as they grow in complexity, can become challenging to navigate. Understanding how different modules and packages interact is crucial for maintenance, refactoring, and onboarding new team members. This is where dependency graphs come into play. A dependency graph visually represents the relationships between components in your codebase, showing which modules rely on others.

While manually mapping these relationships can be tedious and error-prone, tools exist to automate the process. For Python, two powerful tools stand out: pydeps for extracting dependency information and Graphviz for rendering the visualizations.

Why Visualize Python Dependencies?

  • Clarity: Instantly grasp the structure and flow of your application.

  • Refactoring: Identify tightly coupled modules that might be hard to change.

  • Onboarding: Help new developers quickly understand the codebase.

  • Debugging: Trace the path of data or control flow through different components.

  • Optimization: Pinpoint unnecessary or circular dependencies that could cause issues.

At ArchToCode, we understand the importance of clear code visualization. Our AI-powered platform takes code analysis to the next level, automatically generating interactive diagrams from your repositories. While pydeps and Graphviz offer a robust command-line solution, ArchToCode provides a seamless, intuitive web experience for a wide array of diagram types, from simple flowcharts to complex architecture diagrams.

Getting Started: Installing pydeps and Graphviz

Before you can generate your first dependency graph, you need to install both pydeps and Graphviz. pydeps is a Python package, making its installation straightforward with pip. Graphviz, however, is a separate software package that needs to be installed on your system.

1. Install pydeps

Open your terminal or command prompt and run the following command:

pip install pydeps

This command will download and install pydeps and its Python dependencies.

2. Install Graphviz

Graphviz is available for all major operating systems. Follow the instructions for your specific OS:

macOS

The easiest way to install Graphviz on macOS is using Homebrew:

brew install graphviz

Windows

  1. Download the latest stable release installer from the Graphviz download page.

  2. Run the installer. During installation, make sure to check the option to "Add Graphviz to the system PATH for all users" or similar, as this allows pydeps to find the dot command.

  3. If you missed adding it to PATH, you'll need to do it manually via System Properties > Environment Variables.

Linux (Debian/Ubuntu)

On Debian-based systems like Ubuntu, you can install Graphviz using apt:

sudo apt-get update
sudo apt-get install graphviz

Once both are installed, you can verify their installation by running:

pydeps --version
dot -V

You should see version numbers for both tools.

Generating Your First Python Dependency Graph

Now that you have everything set up, let's generate a dependency graph for a simple Python project. For this example, let's assume you have a project structure like this:

my_project/
├── main.py
├── module_a.py
└── subpackage/
    ├── __init__.py
    └── module_b.py

And the content:

# main.py
import module_a
from subpackage import module_b

def main():
    module_a.func_a()
    module_b.func_b()

if __name__ == "__main__":
    main()

# module_a.py
import os

def func_a():
    print("Function A")

# subpackage/module_b.py
import module_a

def func_b():
    module_a.func_a()
    print("Function B")

Basic Usage

Navigate to the root directory of your my_project in the terminal. To generate a basic dependency graph, simply run pydeps followed by the entry point of your project:

pydeps main.py

By default, pydeps will generate an SVG file named pyde.svg in your current directory. Open this file in your web browser, and you'll see a visual representation of your project's dependencies.

Customizing Your Graph

pydeps offers a wide range of options to customize the output. Here are a few common ones:

  • Output File Name: Use -o to specify the output file.

pydeps main.py -o my_project_deps.svg
  • Maximum Depth: Limit the depth of dependencies shown with --max-depth.

pydeps main.py --max-depth 1
  • Exclude Modules: Hide specific modules or packages using -x. This is useful for filtering out standard library modules or external dependencies you're not interested in.

pydeps main.py -x os -x module_a
  • Include External Dependencies: By default, pydeps focuses on internal project dependencies. Use --show-external to include external packages.

pydeps main.py --show-external
  • Reverse Dependencies: See which modules depend on a specific module using --reverse.

pydeps subpackage/module_b.py --reverse
  • Dot Language Output: If you want to manually edit the Graphviz DOT language, use --dot.

pydeps main.py --dot > my_project_deps.dot

You can then render this .dot file to an image using the dot command:

dot -Tsvg my_project_deps.dot -o my_project_deps.svg

Advanced Techniques and Best Practices

Handling Large Projects

For very large projects, a single dependency graph can become overwhelming. Here are some strategies:

  • Focus on Subsystems: Generate graphs for specific sub-packages or modules rather than the entire project.

  • Use --max-depth: Start with a low depth and gradually increase it to explore dependencies layer by layer.

  • Exclude Noise: Aggressively use -x to filter out common utilities, test files, or third-party libraries that clutter the graph.

  • Interactive Exploration: While pydeps generates static images, ArchToCode offers interactive diagrams where you can click nodes to highlight connections, zoom, and pan, making large diagrams much more manageable.

Integrating with Your Workflow

Consider adding dependency graph generation to your CI/CD pipeline or as a pre-commit hook. This can help you:

  • Detect Circular Dependencies: Prevent them from being introduced into your codebase.

  • Monitor Complexity: Track changes in module coupling over time.

Beyond Static Images with ArchToCode

While pydeps and Graphviz are excellent for generating static dependency graphs, ArchToCode provides a dynamic, AI-powered solution for visualizing your code. Imagine connecting your GitHub repository or local folder and having ArchToCode automatically generate a class diagram, a component interaction flowchart, or even a detailed sequence diagram based on your code.

"ArchToCode's AI-powered diagram generation is a game-changer for understanding complex codebases. It goes beyond simple dependency graphs to provide a holistic view of your architecture."

ArchToCode offers:

  • AI Diagram Generation: Use 30+ built-in prompt templates to generate 16 different diagram types, including flowcharts, sequence diagrams, class diagrams, and more.

  • Repository Integration: Connect GitHub or local folders for seamless code analysis.

  • Interactive Diagrams: Click nodes to highlight connections, hover for tooltips, and easily zoom and pan.

  • Source Code Explorer: View the source code associated with diagram nodes, with intelligent file relationship analysis.

  • Error Recovery System: AI-powered diagnosis and automated regeneration for Mermaid syntax errors.

  • Customization: Choose from 20+ themes and 22+ diagram styles to tailor your visualizations.

Instead of manually running commands, ArchToCode allows you to simply select a prompt like "Module Dependencies" or "High-Level Project Flow," and it handles the rest, providing an immediate, interactive visualization. You can even generate sub-diagrams from specific nodes, drilling down into the details of your architecture.

For a deeper dive into how ArchToCode can revolutionize your code understanding, check out our YouTube channel for tutorials and demonstrations.

Conclusion

Generating Python dependency graphs with pydeps and Graphviz is a powerful technique for gaining insights into your codebase's structure. It helps you identify relationships, manage complexity, and ensure the health of your project. By understanding how to install and use these tools, you're well on your way to better code comprehension.

For those seeking an even more advanced and integrated solution, ArchToCode offers an AI-powered platform that automates and enhances the entire code visualization process, providing interactive, customizable diagrams with intelligent analysis. Whether you prefer command-line tools or a sophisticated web application, visualizing your code's architecture is an invaluable practice for any Python developer.

Ready to visualize your code with AI?

Try ArchToCode today and transform your codebase into clear, actionable diagrams. Get started for free!

Related posts