High severity beginner · Fix: 2-5 min

RuntimeError

torch.multiprocessing.spawn.RuntimeError

What this error means
PyTorch raises RuntimeError when the DataLoader's num_workers spawn context is misconfigured or incompatible with the platform's multiprocessing start method.

Stack trace

traceback
RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.
To use the 'spawn' start method, the main module must be importable by the child process.

This usually means you are not using the 'if __name__ == "__main__"' guard in your script.
QUICK FIX
Add 'if __name__ == "__main__"' guard around your DataLoader and training code to fix the spawn context RuntimeError immediately.

Why it happens

PyTorch DataLoader uses multiprocessing to load data in parallel. On platforms like Windows or macOS, the default start method is 'spawn', which requires the main script to be importable and guarded by 'if __name__ == "__main__"'. Without this guard, child processes cannot safely import the main module, causing this RuntimeError.

Detection

Check if your script uses DataLoader with num_workers > 0 and lacks the 'if __name__ == "__main__"' guard. Running the script without this guard on Windows/macOS triggers this error immediately.

Causes & fixes

1

Missing 'if __name__ == "__main__"' guard around DataLoader code using num_workers > 0

✓ Fix

Wrap your DataLoader creation and training loop inside 'if __name__ == "__main__"' block to ensure safe multiprocessing spawn context.

2

Setting num_workers > 0 in an interactive environment like Jupyter Notebook or IPython

✓ Fix

Set num_workers=0 in interactive environments or run your code as a standalone script with the proper main guard.

3

Using incompatible multiprocessing start method or manually setting start method incorrectly

✓ Fix

Use torch.multiprocessing.set_start_method('spawn') at the start of your script inside the main guard, or rely on default platform start method.

Code: broken vs fixed

Broken - triggers the error
python
from torch.utils.data import DataLoader

dataset = ...
dataloader = DataLoader(dataset, num_workers=4)  # RuntimeError here
for batch in dataloader:
    process(batch)
Fixed - works correctly
python
import os
from torch.utils.data import DataLoader

if __name__ == "__main__":
    os.environ['PYTHONWARNINGS'] = 'ignore'  # optional
    dataset = ...
    dataloader = DataLoader(dataset, num_workers=4)  # Fixed: inside main guard
    for batch in dataloader:
        process(batch)

print("DataLoader with num_workers works correctly.")
Wrapped DataLoader code inside 'if __name__ == "__main__"' guard to ensure safe multiprocessing spawn context on Windows/macOS.

Workaround

Set num_workers=0 to disable multiprocessing in DataLoader temporarily when you cannot modify the script structure or run in interactive environments.

Prevention

Always structure PyTorch scripts with 'if __name__ == "__main__"' guard when using DataLoader with num_workers > 0, and avoid multiprocessing in interactive notebooks.

Python 3.7+ · torch >=1.0.0 · tested on 2.0.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.