RuntimeError
torch.multiprocessing.spawn.RuntimeError
Stack trace
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.
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
Missing 'if __name__ == "__main__"' guard around DataLoader code using num_workers > 0
Wrap your DataLoader creation and training loop inside 'if __name__ == "__main__"' block to ensure safe multiprocessing spawn context.
Setting num_workers > 0 in an interactive environment like Jupyter Notebook or IPython
Set num_workers=0 in interactive environments or run your code as a standalone script with the proper main guard.
Using incompatible multiprocessing start method or manually setting start method incorrectly
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
from torch.utils.data import DataLoader
dataset = ...
dataloader = DataLoader(dataset, num_workers=4) # RuntimeError here
for batch in dataloader:
process(batch) 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.") 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.