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
@propertyView on GitHub (pinned to 9fed5c27d2)
Solutions
- Install one of the backends: `pip install tensorboard` (or `pip install tensorboardX`)
- If installation is broken, reinstall/upgrade: `pip install -U tensorboard` and check the embedded import error in the message
- 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
- Add tensorboard to training environment dependencies
- Pin tensorboard version to avoid protobuf conflicts
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
- '{name}' is already present in the registry. HINT: Use `over
- you tried to log {v} which is currently not supported. Try a
- str(_TRANSFORMER_ENGINE_AVAILABLE)
- str(_XLA_AVAILABLE)
- To use the `DeepSpeedStrategy`, you must have DeepSpeed inst
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/44cb0825b90edb1c.
Report an issue: GitHub.