Lightning-AI/pytorch-lightning · critical · ModuleNotFoundError

raise ModuleNotFoundError(str(_XLA_AVAILABLE))

Error message

raise ModuleNotFoundError(str(_XLA_AVAILABLE))

What it means

The XLA launcher (_XLALauncher) requires torch_xla. Lightning gates imports via the _XLA_AVAILABLE message; if torch_xla is not installed, constructing the launcher raises ModuleNotFoundError with that explanatory message.

Source

Thrown at src/lightning/pytorch/strategies/launchers/xla.py:55

    r"""Launches processes that run a given function in parallel on XLA supported hardware, and joins them all at the
    end.

    The main process in which this launcher is invoked creates N so-called worker processes (using the
    `torch_xla` :func:`xmp.spawn`) that run the given function.
    Worker processes have a rank that ranges from 0 to N - 1.

    Note:
        - This launcher requires all objects to be pickleable.
        - It is important that the entry point to the program/script is guarded by ``if __name__ == "__main__"``.

    Args:
        strategy: A reference to the strategy that is used together with this launcher

    """

    def __init__(self, strategy: "pl.strategies.XLAStrategy") -> None:
        if not _XLA_AVAILABLE:
            raise ModuleNotFoundError(str(_XLA_AVAILABLE))
        super().__init__(strategy=strategy, start_method="fork")

    @property
    @override
    def is_interactive_compatible(self) -> bool:
        return True

    @override
    def launch(self, function: Callable, *args: Any, trainer: Optional["pl.Trainer"] = None, **kwargs: Any) -> Any:
        """Launches processes that run the given function in parallel.

        The function is allowed to have a return value. However, when all processes join, only the return value
        of worker process 0 gets returned from this `launch` method in the main process.

        Arguments:
            function: The entry point for all launched processes.
            *args: Optional positional arguments to be passed to the given function.
            trainer: Optional reference to the :class:`~lightning.pytorch.trainer.trainer.Trainer` for which

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Install torch_xla matching your torch version (e.g. pip install torch_xla==<matching version>) or the extra: pip install lightning[xla]
  2. Verify with python -c "import torch_xla"
  3. If you didn't intend XLA, switch the strategy/accelerator to the CPU/GPU ones

Example fix

# before
strategy = XLAStrategy()  # ModuleNotFoundError without torch_xla

# after
pip install lightning[xla]
strategy = XLAStrategy()
Defensive patterns

Strategy: validation

Validate before calling

from lightning.fabric.utilities.imports import _XLA_AVAILABLE
assert _XLA_AVAILABLE, "install torch_xla / pip install lightning[xla]"

Prevention

When it happens

Trigger: Instantiating XLAStrategy (which creates an _XLALauncher) without torch_xla installed, e.g. on a GPU box or CI without XLA libs; using lightning-cloud / accelerator='tpu' without the xla extra.

Common situations: Selecting the TPU/XLA strategy in a plain PyTorch environment; missing 'lightning[xla]' or torch_xla wheel for the PyTorch version in use.

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/72576b160a2619a8. Report an issue: GitHub.