{"record":{"id":"129007a9f7941f1b","repo":"Lightning-AI/pytorch-lightning","slug":"raise-modulenotfounderror-str-xla-available-129007","errorCode":null,"errorMessage":"raise ModuleNotFoundError(str(_XLA_AVAILABLE))","messagePattern":"raise ModuleNotFoundError\\(str\\(_XLA_AVAILABLE\\)\\)","errorType":"exception","errorClass":"ModuleNotFoundError","httpStatus":null,"severity":"critical","filePath":"src/lightning/pytorch/strategies/xla.py","lineNumber":60,"sourceCode":"\nclass XLAStrategy(DDPStrategy):\n    \"\"\"Strategy for training multiple TPU devices using the :func:`torch_xla.distributed.xla_multiprocessing.spawn`\n    method.\"\"\"\n\n    strategy_name = \"xla\"\n\n    def __init__(\n        self,\n        accelerator: Optional[\"pl.accelerators.Accelerator\"] = None,\n        parallel_devices: Optional[list[torch.device]] = None,\n        checkpoint_io: Optional[Union[XLACheckpointIO, _WrappingCheckpointIO]] = None,\n        precision_plugin: Optional[XLAPrecision] = None,\n        debug: bool = False,\n        sync_module_states: bool = True,\n        **_: Any,\n    ) -> None:\n        if not _XLA_AVAILABLE:\n            raise ModuleNotFoundError(str(_XLA_AVAILABLE))\n        super().__init__(\n            accelerator=accelerator,\n            parallel_devices=parallel_devices,\n            cluster_environment=XLAEnvironment(),\n            checkpoint_io=checkpoint_io,\n            precision_plugin=precision_plugin,\n            start_method=\"fork\",\n        )\n        self.debug = debug\n        self._launched = False\n        self._sync_module_states = sync_module_states\n\n    @property\n    @override\n    def checkpoint_io(self) -> Union[XLACheckpointIO, _WrappingCheckpointIO]:\n        plugin = self._checkpoint_io\n        if plugin is not None:\n            assert isinstance(plugin, (XLACheckpointIO, _WrappingCheckpointIO))","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/strategies/xla.py#L42-L78","documentation":"XLAStrategy requires torch_xla. Its __init__ raises ModuleNotFoundError with Lightning's _XLA_AVAILABLE explanatory message when the package is absent, before setting up the XLA cluster environment and defaults.","triggerScenarios":"Constructing XLAStrategy(...) in an environment without torch_xla (local GPU/CPU box, CI, or a container missing the XLA wheel).","commonSituations":"Running TPU-targeted scripts locally for debugging; requirements.txt missing torch_xla; version mismatch after upgrading torch.","solutions":["Install torch_xla matching your torch version, or pip install lightning[xla]","Confirm python -c \"import torch_xla\" works in the same interpreter/environment the script uses","If not targeting TPU, use DDPStrategy/SingleDeviceStrategy"],"exampleFix":"# before\nstrategy = XLAStrategy()  # ModuleNotFoundError\n\n# after\n# pip install lightning[xla]\nstrategy = XLAStrategy()","handlingStrategy":"validation","validationCode":"from lightning.fabric.utilities.imports import _XLA_AVAILABLE\nassert _XLA_AVAILABLE, \"pip install lightning[xla] before using XLAStrategy\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pin torch_xla versions consistent with torch in requirements","Smoke-test `import torch_xla` in CI for TPU jobs"],"tags":["xla","tpu","missing-dependency"],"backgroundTag":"missing-optional-dependency","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}