pydeps

3.0.2 · active · verified Thu Apr 16

pydeps is a Python library and command-line tool for visualizing module dependencies within Python projects. It analyzes import statements to build a dependency graph and can output it in various formats (e.g., SVG, PNG) using Graphviz. The current version is 3.0.2, and it follows an active release cadence, often updating for Python compatibility or bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use pydeps from the command line (via subprocess) to visualize module dependencies. It creates a temporary Python package, runs pydeps on it, and prints the raw dependency list. To generate a visual graph (e.g., SVG), ensure Graphviz is installed and remove the `--nodot --no-output` flags, using `-o filename.svg` instead.

import os
import subprocess
import tempfile

def create_sample_project(path):
    os.makedirs(os.path.join(path, 'mypackage'), exist_ok=True)
    with open(os.path.join(path, 'mypackage', '__init__.py'), 'w') as f:
        f.write('')
    with open(os.path.join(path, 'mypackage', 'module_a.py'), 'w') as f:
        f.write('import os\nfrom . import module_b\ndef func_a():\n    print("Hello from A")')
    with open(os.path.join(path, 'mypackage', 'module_b.py'), 'w') as f:
        f.write('import sys\ndef func_b():\n    print("Hello from B")')

with tempfile.TemporaryDirectory() as tmpdir:
    create_sample_project(tmpdir)
    current_dir = os.getcwd()
    os.chdir(tmpdir)
    
    try:
        # Run pydeps as a subprocess to generate the dependency graph
        # Output to stdout and catch errors
        result = subprocess.run(
            ['pydeps', 'mypackage', '--nodot', '--no-output', '--show-deps'],
            capture_output=True, text=True, check=True
        )
        print("\n--- pydeps output (dependencies) ---")
        print(result.stdout)
        
        # To generate an SVG file, you'd typically run:
        # subprocess.run(['pydeps', 'mypackage', '-o', 'mypackage.svg'], check=True)
        # print(f"Dependency graph saved to {os.path.join(tmpdir, 'mypackage.svg')}")
        
    except FileNotFoundError:
        print("Error: 'pydeps' command not found. Ensure pydeps is installed and in PATH.")
        print("Error: 'dot' command (from Graphviz) might also be missing. Install Graphviz.")
    except subprocess.CalledProcessError as e:
        print(f"Error running pydeps: {e}")
        print(f"Stdout: {e.stdout}")
        print(f"Stderr: {e.stderr}")
    finally:
        os.chdir(current_dir)

view raw JSON →