Even though the question is old and the existing answer is correct, I would like to expand the discussion since I lost myself a considerate amount of time understanding this problem.
joblib
is a package that allows for object persistence similar to pickle
. These objects are instances of classes, e.g. typically a dataframe is an instance of pandas.DataFrame
class. If you serialize this object, i.e. joblib.dump(“my_df.joblib”)
, the object is stored in a binary file which is tagged by its class name(s).
If you deserialize the file in order to get back the object, i.e. joblib.load(“my_df.joblib”)
, Python has to search for the class definition in order to be able to instantiate it. To keep up with our dataframe example, this would correspond to pandas.DataFrame
. So if in the current context (different script, different notebook, etc.) you do not have pandas
installed, you will get the famous ModuleNotFoundError
since Python does not know how to instantiate your dataframe.
Now you have to transfer this intuition to your custom class: If its definition is within the main module when you create it, then this same definition must be in the main module the moment you load it. In your case that is __main__.Duck
which means you'd need to copy-paste your class definition into your new notebook. This is, however, not a very practical approach. So I suggest you create an additional module, e.g. a folder named utils
or similar where you put in all your scripts containing custom classes. The structure could then look like this:
yourProject/
│
├── notebook.ipynb
├── utils/
| ├── __init__.py
| └── animals.py
|
└── my_duck.joblib
And within the animals.py
:
class Duck():
def quack(self):
print("Quack!")
If you import the custom class now, you would use:
from utils.animals import Duck
Like so, joblib
tags the object accordingly and you can just use your custom module (e.g. copy-paste the utils folder) when you load your object elsewhere. Just make sure that the relative paths stay exactly the same as Python will search in ./utils/animals.py
when importing Duck
.