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
- pip install -U rich
- Or use the plain ModelSummary callback which needs no extra dependency
- 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
- Pin `rich` in requirements if you use any Rich* callbacks
- Add an import check before constructing Rich callbacks
- Prefer plain ModelSummary in shared/base environments
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
- `RichProgressBar` requires `rich` >= 10.2.2. Install it by r
- outputs have to be of type torch.Tensor or Mapping, got {typ
- swa_epoch_start should be a >0 integer or a float between 0
- The `avg_fn` should be callable.
- device is expected to be a torch.device or a str. Found {dev
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/b84102aeb3ef53d4.
Report an issue: GitHub.