{"record":{"id":"f264533b4161ace7","repo":"Lightning-AI/pytorch-lightning","slug":"this-script-was-launched-through-the-cli-and-proc","errorCode":null,"errorMessage":"This script was launched through the CLI, and processes have already been created. Calling  `.launch()` again is not allowed.","messagePattern":"This script was launched through the CLI, and processes have already been created\\. Calling  `\\.launch\\(\\)` again is not allowed\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/fabric.py","lineNumber":990,"sourceCode":"        Note:\n            The ``launch()`` method should only be used if you intend to specify accelerator, devices, and so on in\n            the code (programmatically). If you are launching with the Lightning CLI, ``fabric run ...``, remove\n            ``launch()`` from your code.\n\n            The ``launch()`` is a no-op when called multiple times and no function is passed in.\n\n        Example::\n\n            def train_function(fabric):\n                model, optimizer = fabric.setup(model, optimizer)\n                # ... training code ...\n\n            fabric = Fabric(accelerator=\"tpu\", devices=8)\n            fabric.launch(train_function)\n\n        \"\"\"\n        if _is_using_cli():\n            raise RuntimeError(\n                \"This script was launched through the CLI, and processes have already been created. Calling \"\n                \" `.launch()` again is not allowed.\"\n            )\n        if function is not _do_nothing:\n            if not callable(function):\n                raise TypeError(\n                    f\"`Fabric.launch(...)` needs to be a callable, but got {function}.\"\n                    \" HINT: do `.launch(your_fn)` instead of `.launch(your_fn())`\"\n                )\n            if not inspect.signature(function).parameters:\n                raise TypeError(\n                    f\"`Fabric.launch(function={function})` needs to take at least one argument. The launcher will\"\n                    \" pass in the `Fabric` object so you can use it inside the function.\"\n                )\n        elif isinstance(self.strategy.launcher, (_MultiProcessingLauncher, _XLALauncher)):\n            raise TypeError(\n                f\"To spawn processes with the `{type(self.strategy).__name__}` strategy, `.launch()` needs to be called\"\n                \" with a function that contains the code to launch in processes.\"","sourceCodeStart":972,"sourceCodeEnd":1008,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/fabric.py#L972-L1008","documentation":"When a script is started via the Lightning CLI (`fabric run script.py`), the launcher already spawns/sets up processes in __init__. Calling fabric.launch() again would attempt a second, nested launch, so Fabric raises RuntimeError to prevent it.","triggerScenarios":"Writing `if __name__ == '__main__': fabric.launch(main)` in a script executed with `fabric run ...`. The CLI path sets _is_using_cli(), making the explicit launch call illegal.","commonSituations":"Converting a standalone Fabric script to CLI-driven execution and leaving the old launch() call in; copy-pasting the plain-run example into a CLI-launched script.","solutions":["Delete the fabric.launch() call — with the CLI, the CLI invokes your code automatically","Run the script with plain `python script.py` if you want to keep explicit .launch()"],"exampleFix":"# before (run with: fabric run script.py)\nfabric.launch(train_fn)  # raises\n# after\n# no launch() call; the CLI drives the script","handlingStrategy":"validation","validationCode":"from lightning.fabric.utilities.imports import _is_using_cli\nif not _is_using_cli():\n    fabric.launch(train)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pick one launch mechanism per script: CLI or explicit .launch(), never both"],"tags":["lightning","fabric","cli","launch","double-launch"],"backgroundTag":"double-initialization","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}