{"record":{"id":"a543a86fd7019cc7","repo":"Lightning-AI/pytorch-lightning","slug":"trainer-strategy-self-strategy-flag-r-is-not","errorCode":null,"errorMessage":"`Trainer(strategy={self._strategy_flag!r})` is not compatible with an interactive environment. Run your code as a script, or choose a notebook-compatible strategy: `Trainer(strategy='ddp_notebook')`. In case you are spawning processes yourself, make sure to include the Trainer creation inside the worker function.","messagePattern":"`Trainer\\(strategy=(.+?)\\)` is not compatible with an interactive environment\\. Run your code as a script, or choose a notebook-compatible strategy: `Trainer\\(strategy='ddp_notebook'\\)`\\. In case you are spawning processes yourself, make sure to include the Trainer creation inside the worker function\\.","errorType":"validation","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/connectors/accelerator_connector.py","lineNumber":535,"sourceCode":"        if hasattr(self.strategy, \"cluster_environment\"):\n            if self.strategy.cluster_environment is None:\n                self.strategy.cluster_environment = self.cluster_environment\n            self.cluster_environment = self.strategy.cluster_environment\n        if hasattr(self.strategy, \"parallel_devices\"):\n            if self.strategy.parallel_devices:\n                self._parallel_devices = self.strategy.parallel_devices\n            else:\n                self.strategy.parallel_devices = self._parallel_devices\n        if hasattr(self.strategy, \"num_nodes\"):\n            self.strategy.num_nodes = self._num_nodes_flag\n        if hasattr(self.strategy, \"_layer_sync\"):\n            self.strategy._layer_sync = self._layer_sync\n        if hasattr(self.strategy, \"set_world_ranks\"):\n            self.strategy.set_world_ranks()\n        self.strategy._configure_launcher()\n\n        if _IS_INTERACTIVE and self.strategy.launcher and not self.strategy.launcher.is_interactive_compatible:\n            raise MisconfigurationException(\n                f\"`Trainer(strategy={self._strategy_flag!r})` is not compatible with an interactive\"\n                \" environment. Run your code as a script, or choose a notebook-compatible strategy:\"\n                f\" `Trainer(strategy='ddp_notebook')`.\"\n                \" In case you are spawning processes yourself, make sure to include the Trainer\"\n                \" creation inside the worker function.\"\n            )\n\n        # TODO: should be moved to _check_strategy_and_fallback().\n        # Current test check precision first, so keep this check here to meet error order\n        if isinstance(self.accelerator, XLAAccelerator) and not isinstance(\n            self.strategy, (SingleDeviceXLAStrategy, XLAStrategy)\n        ):\n            raise ValueError(\n                \"The `XLAAccelerator` can only be used with a `SingleDeviceXLAStrategy` or `XLAStrategy`,\"\n                f\" found {self.strategy.__class__.__name__}.\"\n            )\n\n    @property","sourceCodeStart":517,"sourceCodeEnd":553,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/connectors/accelerator_connector.py#L517-L553","documentation":"Certain strategies spawn subprocesses via a launcher that is not compatible with interactive environments (Jupyter/IPython). If Lightning detects an interactive session (sys.modules/psutil heuristics) and the configured launcher lacks is_interactive_compatible, it raises MisconfigurationException during strategy init.","triggerScenarios":"Trainer(strategy='ddp') (or ddp_spawn/deepspeed etc. with non-interactive launchers) inside Jupyter/IPython/VS Code interactive windows.","commonSituations":"Prototyping distributed training in notebooks; tutorials using ddp in Colab; running ddp scripts line-by-line in IDE consoles.","solutions":["Use Trainer(strategy='ddp_notebook') (or 'ddp_fork' style interactive-compatible strategy) inside notebooks","Move Trainer creation and training into a .py script and run it with python/launcher outside the interactive session","If spawning processes yourself, create the Trainer inside the worker function as the message advises"],"exampleFix":"# before\n# in Jupyter\ntrainer = Trainer(strategy=\"ddp\", accelerator=\"gpu\", devices=2)\n# after\n# in Jupyter\ntrainer = Trainer(strategy=\"ddp_notebook\", accelerator=\"gpu\", devices=2)","handlingStrategy":"fallback","validationCode":"import sys\nINTERACTIVE = \"ipykernel\" in sys.modules or \"IPython\" in sys.modules\nif INTERACTIVE and strategy in (\"ddp\", \"ddp_spawn\", \"deepspeed\"):\n    strategy = \"ddp_notebook\"\ntrainer = Trainer(strategy=strategy)","typeGuard":"def is_interactive() -> bool:\n    import sys\n    return \"ipykernel\" in sys.modules or \"IPython\" in sys.modules","tryCatchPattern":"from lightning.pytorch.utilities.exceptions import MisconfigurationException\ntry:\n    trainer = Trainer(strategy=\"ddp\")\nexcept MisconfigurationException as e:\n    if \"interactive\" in str(e):\n        trainer = Trainer(strategy=\"ddp_notebook\")\n    else:\n        raise","preventionTips":["Keep Trainer construction inside a main() function in .py scripts for distributed runs","Select strategy based on environment: ddp_notebook in notebooks, ddp in scripts"],"tags":["pytorch-lightning","ddp","jupyter","interactive-environment"],"backgroundTag":"distributed-notebook-incompatible","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}