Lightning-AI/pytorch-lightning · error · ModuleNotFoundError

`RichModelSummary` requires `rich` to be installed. Install

Error message

`RichModelSummary` requires `rich` to be installed. Install it by running `pip install -U rich`.

What it means

RichModelSummary is a ModelSummary callback that renders the model hierarchy table using the `rich` library. On construction it checks whether rich is importable and, if not, raises ModuleNotFoundError telling the user to install rich. It is purely an optional-dependency guard.

Source

Thrown at src/lightning/pytorch/callbacks/rich_model_summary.py:62

        from lightning.pytorch import Trainer
        from lightning.pytorch.callbacks import RichProgressBar

        trainer = Trainer(callbacks=RichProgressBar())

    Args:
        max_depth: The maximum depth of layer nesting that the summary will include. A value of 0 turns the
            layer summary off.
        **summarize_kwargs: Additional arguments to pass to the `summarize` method.

    Raises:
        ModuleNotFoundError:
            If required `rich` package is not installed on the device.

    """

    def __init__(self, max_depth: int = 1, **summarize_kwargs: Any) -> None:
        if not _RICH_AVAILABLE:
            raise ModuleNotFoundError(
                "`RichModelSummary` requires `rich` to be installed. Install it by running `pip install -U rich`."
            )
        super().__init__(max_depth, **summarize_kwargs)

    @staticmethod
    @override
    def summarize(
        summary_data: list[tuple[str, list[str]]],
        total_parameters: int,
        trainable_parameters: int,
        model_size: float,
        total_training_modes: dict[str, int],
        total_flops: int,
        **summarize_kwargs: Any,
    ) -> None:
        from rich import get_console
        from rich.table import Table

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. pip install -U rich
  2. Or use the plain ModelSummary callback which needs no extra dependency
  3. Or install the relevant extras, e.g. pip install lightning[rich] if provided

Example fix

// before
trainer = Trainer(callbacks=[RichModelSummary()])  # ModuleNotFoundError: rich missing
// after
# pip install -U rich
trainer = Trainer(callbacks=[RichModelSummary()])
Defensive patterns

Strategy: validation

Validate before calling

from lightning.fabric.utilities.imports import _RICH_AVAILABLE  # or importlib.util.find_spec
import importlib.util
rich_ok = importlib.util.find_spec("rich") is not None
if not rich_ok:
    callbacks = [ModelSummary(max_depth=2)]  # fallback
else:
    callbacks = [RichModelSummary(max_depth=2)]

Prevention

When it happens

Trigger: Instantiating RichModelSummary(max_depth=...) or passing it to Trainer(callbacks=[RichModelSummary()]) in an environment where the `rich` package is not installed (it is not a hard dependency of lightning).

Common situations: Fresh environments or minimal installs (pip install lightning without extras), CI docker images that prune optional deps, or using RichProgressBar/RichModelSummary after switching to a lighter virtualenv.

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 Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/b84102aeb3ef53d4. Report an issue: GitHub.