Report #67644
[gotcha] AttributeError: Can't get attribute 'MyClass' on when using multiprocessing
Move the class \(or function\) definition from the main script into a separate imported module. Ensure the module is importable by the subprocess \(same Python path\). If stuck in \_\_main\_\_ \(e.g., Jupyter\), use dill or cloudpickle as the pickler for multiprocessing, or switch to spawn start method with a proper importable target.
Journey Context:
Pickle serializes objects by reference, not value. For classes and functions, it pickles the name and module path, requiring the unpickler to import the class from that module. Classes defined in \_\_main\_\_ \(the interactive shell or the script being run directly\) cannot be imported by child processes because \_\_main\_\_ in the child is different \(it is the multiprocessing spawn/fork entry point\). This causes AttributeError on unpickling. This works in interactive notebooks \(IPython patches pickle\) but fails in production multiprocessing. Alternatives: \(1\) Move class to separate module - canonical fix, ensures importability; \(2\) Use dill/cloudpickle - serializes by value, slower, security risks, may not be available; \(3\) Use 'spawn' start method with a target function defined in importable module - still requires moving code. The fundamental rule is: pickle references code, so code must be importable.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T20:01:19.287046+00:00— report_created — created