Lightning-AI/pytorch-lightning · error · RuntimeError

This script was launched through the CLI, and processes have

Error message

This script was launched through the CLI, and processes have already been created. Calling  `.launch()` again is not allowed.

What it means

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.

Source

Thrown at src/lightning/fabric/fabric.py:990

        Note:
            The ``launch()`` method should only be used if you intend to specify accelerator, devices, and so on in
            the code (programmatically). If you are launching with the Lightning CLI, ``fabric run ...``, remove
            ``launch()`` from your code.

            The ``launch()`` is a no-op when called multiple times and no function is passed in.

        Example::

            def train_function(fabric):
                model, optimizer = fabric.setup(model, optimizer)
                # ... training code ...

            fabric = Fabric(accelerator="tpu", devices=8)
            fabric.launch(train_function)

        """
        if _is_using_cli():
            raise RuntimeError(
                "This script was launched through the CLI, and processes have already been created. Calling "
                " `.launch()` again is not allowed."
            )
        if function is not _do_nothing:
            if not callable(function):
                raise TypeError(
                    f"`Fabric.launch(...)` needs to be a callable, but got {function}."
                    " HINT: do `.launch(your_fn)` instead of `.launch(your_fn())`"
                )
            if not inspect.signature(function).parameters:
                raise TypeError(
                    f"`Fabric.launch(function={function})` needs to take at least one argument. The launcher will"
                    " pass in the `Fabric` object so you can use it inside the function."
                )
        elif isinstance(self.strategy.launcher, (_MultiProcessingLauncher, _XLALauncher)):
            raise TypeError(
                f"To spawn processes with the `{type(self.strategy).__name__}` strategy, `.launch()` needs to be called"
                " with a function that contains the code to launch in processes."

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Delete the fabric.launch() call — with the CLI, the CLI invokes your code automatically
  2. Run the script with plain `python script.py` if you want to keep explicit .launch()

Example fix

# before (run with: fabric run script.py)
fabric.launch(train_fn)  # raises
# after
# no launch() call; the CLI drives the script
Defensive patterns

Strategy: validation

Validate before calling

from lightning.fabric.utilities.imports import _is_using_cli
if not _is_using_cli():
    fabric.launch(train)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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