Lightning-AI/pytorch-lightning · error · ModuleNotFoundError

{str(_XLA_AVAILABLE)}

Error message

{str(_XLA_AVAILABLE)}

What it means

The XLA launcher requires torch_xla, and at import time Lightning captured the missing-dependency ModuleNotFound message in _XLA_AVAILABLE. Constructing the XLALauncher without torch_xla installed re-raises that stored import error, typically telling you the 'torch_xla' package is missing.

Source

Thrown at src/lightning/fabric/strategies/launchers/xla.py:49

    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: Union["XLAStrategy", "XLAFSDPStrategy"]) -> None:
        if not _XLA_AVAILABLE:
            raise ModuleNotFoundError(str(_XLA_AVAILABLE))
        self._strategy = strategy
        self._start_method = "fork"

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

    @override
    def launch(self, function: Callable, *args: Any, **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.

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. pip install torch_xla matching your torch version (see PyTorch/XLA release matrix)
  2. If you didn't intend TPU, switch accelerator/strategy to 'cpu' or 'gpu' equivalents
  3. Verify the install with 'python -c "import torch_xla"' to surface any version-mismatch import errors

Example fix

# before
fabric = Fabric(accelerator="tpu", devices=8)  # ModuleNotFoundError: No module named 'torch_xla'

# after
pip install torch_xla==<version matching torch>
fabric = Fabric(accelerator="tpu", devices=8)
Defensive patterns

Strategy: validation

Validate before calling

try:
    import torch_xla  # noqa
    xla_ok = True
except ImportError:
    xla_ok = False
if not xla_ok:
    accelerator = "cpu"  # or fail fast with a clear message

Type guard

def xla_available() -> bool:
    try:
        import torch_xla  # noqa: F401
        return True
    except ImportError:
        return False

Try / catch

try:
    launcher = XLALauncher(strategy)
except ModuleNotFoundError as e:
    if "torch_xla" in str(e):
        subprocess.run([sys.executable, "-m", "pip", "install", "torch_xla"])
    raise

Prevention

When it happens

Trigger: Instantiating XLAStrategy/XLAFSDPStrategy (e.g. Fabric(accelerator='tpu') or strategy='xla...') without torch_xla installed, or with a torch_xla build mismatched to the installed torch version so the import failed.

Common situations: Running TPU code in a CPU/GPU environment; new environments where requirements omitted torch_xla; upgrading torch without rebuilding torch_xla, making the import fail and getting captured as _XLA_AVAILABLE error state.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/95c67e0c758aef69. Report an issue: GitHub.