Lightning-AI/pytorch-lightning · error · ModuleNotFoundError

Neither `tensorboard` nor `tensorboardX` is available. Try `

Error message

Neither `tensorboard` nor `tensorboardX` is available. Try `pip install`ing either.
{TENSORBOARDX_AVAILABLE}
{TENSORBOARD_AVAILABLE}

What it means

The Fabric TensorBoardLogger requires either the `tensorboard` or `tensorboardX` package to write event files; if neither imports successfully, the constructor raises ModuleNotFoundError with the underlying import error messages.

Source

Thrown at src/lightning/fabric/loggers/tensorboard.py:93

        logger.log_metrics({"acc": 0.75})
        logger.finalize("success")

    """

    LOGGER_JOIN_CHAR = "-"

    def __init__(
        self,
        root_dir: _PATH,
        name: Optional[str] = "lightning_logs",
        version: Optional[Union[int, str]] = None,
        default_hp_metric: bool = True,
        prefix: str = "",
        sub_dir: Optional[_PATH] = None,
        **kwargs: Any,
    ):
        if not _TENSORBOARD_AVAILABLE and not _TENSORBOARDX_AVAILABLE:
            raise ModuleNotFoundError(
                "Neither `tensorboard` nor `tensorboardX` is available. Try `pip install`ing either.\n"
                f"{str(_TENSORBOARDX_AVAILABLE)}\n{str(_TENSORBOARD_AVAILABLE)}"
            )
        super().__init__()
        root_dir = os.fspath(root_dir)
        self._root_dir = root_dir
        self._name = name or ""
        self._version = version
        self._sub_dir = None if sub_dir is None else os.fspath(sub_dir)

        self._default_hp_metric = default_hp_metric
        self._prefix = prefix
        self._fs = get_filesystem(root_dir)

        self._experiment: Optional[SummaryWriter] = None
        self._kwargs = kwargs

    @property

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Install one of the backends: `pip install tensorboard` (or `pip install tensorboardX`)
  2. If installation is broken, reinstall/upgrade: `pip install -U tensorboard` and check the embedded import error in the message
  3. Alternatively switch to CSVLogger if TensorBoard is not needed

Example fix

# before
from lightning.fabric.loggers import TensorBoardLogger
logger = TensorBoardLogger('logs/')  # ModuleNotFoundError

# after (shell)
# pip install tensorboard
logger = TensorBoardLogger('logs/')
Defensive patterns

Strategy: validation

Validate before calling

from lightning.fabric.loggers.tensorboard import _TENSORBOARD_AVAILABLE, _TENSORBOARDX_AVAILABLE
if not (_TENSORBOARD_AVAILABLE or _TENSORBOARDX_AVAILABLE):
    from lightning.fabric.loggers import CSVLogger
    logger = CSVLogger('logs')
else:
    from lightning.fabric.loggers import TensorBoardLogger
    logger = TensorBoardLogger('logs')

Try / catch

try:
    from lightning.fabric.loggers import TensorBoardLogger
    logger = TensorBoardLogger('logs')
except ModuleNotFoundError:
    from lightning.fabric.loggers import CSVLogger
    logger = CSVLogger('logs')

Prevention

When it happens

Trigger: Instantiating `TensorBoardLogger(...)` in an environment where neither `tensorboard` nor `tensorboardX` is installed (or both fail to import due to broken installs/dependency conflicts).

Common situations: Fresh environments where only `torch` is installed; slim Docker images for training; prototyping locally without TensorBoard; broken tensorboard installs after upgrading protobuf/numpy.

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/44cb0825b90edb1c. Report an issue: GitHub.