tirth8205/code-review-graph · error · ImportError

matplotlib is required for SVG export. Install with: pip ins

Error message

matplotlib is required for SVG export. Install with: pip install matplotlib

What it means

export_svg() imports matplotlib lazily (with the Agg backend); if matplotlib is not installed the ImportError is re-raised with install instructions. SVG export is an optional dependency.

Source

Thrown at code_review_graph/exports.py:368

    return slug[:100] or "unnamed"


# -------------------------------------------------------------------
# SVG export (matplotlib-based)
# -------------------------------------------------------------------

def export_svg(store: GraphStore, output_path: Path) -> Path:
    """Export a static SVG graph visualization.

    Requires matplotlib (optional dependency).
    Returns the path to the written file.
    """
    try:
        import matplotlib
        matplotlib.use("Agg")
        import matplotlib.pyplot as plt
    except ImportError:
        raise ImportError(
            "matplotlib is required for SVG export. "
            "Install with: pip install matplotlib"
        )

    import networkx as nx

    data = export_graph_data(store)
    nodes_data = data["nodes"]
    edges_data = data["edges"]

    nxg: nx.DiGraph = nx.DiGraph()  # type: ignore[type-arg]
    for n in nodes_data:
        nxg.add_node(
            n["qualified_name"],
            label=n.get("name", ""),
            kind=n.get("kind", ""),
        )
    for e in edges_data:

View on GitHub (pinned to b58668751a)

Solutions

  1. pip install matplotlib
  2. Or add matplotlib to your project's dependencies/requirements
  3. Use export_dot/export_json instead if you can't install matplotlib

Example fix

# before
pip install code-review-graph
# after
pip install code-review-graph matplotlib
Defensive patterns

Strategy: validation

Validate before calling

try:
    import matplotlib  # noqa
except ImportError:
    raise RuntimeError("SVG export unavailable; pip install matplotlib or use export_json")

Type guard

def svg_export_available() -> bool:
    try:
        import matplotlib  # noqa
        return True
    except ImportError:
        return False

Try / catch

try:
    export_svg(store, "out.svg")
except ImportError as e:
    if "matplotlib" in str(e):
        export_json(store, "out.json")  # fallback format
    else:
        raise

Prevention

When it happens

Trigger: Calling export_svg() (e.g. the export --svg CLI path via main) in an environment where matplotlib isn't installed.

Common situations: Installing the package without optional extras, slim Docker images, or assuming networkx alone suffices for visualization.

Understand the failure class

Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.

Related errors


AI-assisted analysis of tirth8205/code-review-graph@b58668751a (2026-08-28). Data as JSON: /api/errors/e2498abcc0b6852a. Report an issue: GitHub.