joblib.load __main__ AttributeError
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A joblib.load error mentioning __main__ usually means the serialized object refers to a class or function that existed in the __main__ module when it was saved, but that same symbol is not available in __main__ when you load it.
This is not really a joblib-specific mystery. It is a serialization import-path problem. joblib relies on Python's pickle mechanics, and pickle needs to import the same class or function by module path when the object is reconstructed.
Why __main__ Causes Trouble
Suppose you train and save a model wrapper in a script where the class is defined directly in that script:
When that script runs, the class lives under __main__.MyModel. If you later load the file from a different entry point, Python may not have MyModel in __main__ anymore, so loading fails with an AttributeError.
Move The Class Into An Importable Module
The durable fix is to define the class or helper function in a normal module, not in the top-level training script.
mymodel.py:
Saving:
Loading:
Now the object can be reconstructed by importing mymodel.MyModel, which is stable across scripts.
Import Before Loading
Sometimes the class is already in a module, but the loading script never imports it. In that case, importing the module before joblib.load can be enough. The larger principle is that the deserializer must be able to resolve the original symbol path.
Best Practice For Models And Pipelines
This issue is common in notebooks and training scripts because people define helper classes inline. That works until the artifact is loaded in a different process, deployment service, or notebook kernel.
The safer pattern is:
- define custom transformers and wrappers in importable modules
- version the code together with the serialized artifact
- load in an environment where those modules are installed and importable
That makes model artifacts much less fragile.
Notebooks Make This Worse
Jupyter notebooks are a common source of this error because training may happen in one notebook session where the class lives in __main__, while loading happens in a different kernel or deployment process. The object file looks fine, but the symbol path has changed underneath it.
Artifacts Depend On Code Version Too
This is also a reminder that serialized Python objects depend on the code that created them. Loading an artifact successfully often requires not just the same module path, but a compatible implementation of the class itself. Treat model files and the code defining custom objects as one deployment unit.
Common Pitfalls
- Defining custom classes only inside a training script or notebook cell.
- Saving objects whose classes live under
__main__and expecting them to load everywhere. - Forgetting to import the defining module before loading.
- Treating the error as corrupted data when it is really a missing symbol problem.
- Assuming
joblibis independent from pickle's import-path rules.
Summary
- A
joblib.load__main__AttributeErrorusually means pickle cannot find the original class or function path. - Objects saved from a top-level script often get recorded under
__main__. - Move custom classes and functions into real importable modules.
- Import the defining module before loading the artifact.
- Treat serialized Python objects as code-dependent artifacts, not as self-contained universal files.
Related reading
- Join a string using delimiters
- join list of lists in python
- Joining columns in pandas incorrectly
- Joining pandas DataFrames by Column names
- jQuery .geturl breaks my sequential script execution
- jQuery's jquery-1.10.2.min.map is triggering a 404 (Not Found)
- Joining string and tf.string to get a path
- JSON datetime between Python and JavaScript
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.