Ondřej Plátek Archive
PhD candidate@UFAL, Prague. LLM & TTS evaluation. Engineer. Researcher. Speaker. Father.

Unknown class after unpickling in ipython

You have pickled something in your modelu and now you can not unpickle it in ipython?
E.g.

# in mymodule.py you have
record=namedtuple('record', 'name img_link format link text')


In ipython you try:

$ import pickle
$ with open('crawled.data','rb') as f:
rds=pickle.load(f)


and you got

AttributeError: 'FakeModule' object has no attribute 'record'


The solution is simple. You have to import the definition from the module, where is defined and you have to register it to the place where the pickle module search for it.
For more explanation see stackoverflow
.
The code is:

$import sys
$import mymodule
$sys.modules['__main__'].record=mymodule.record

$import pickle
$ with open('crawled.data','rb') as f:
rds=pickle.load(f)