Pickle problem while loading a class instance.

Lie Ryan lie.1296 at gmail.com
Tue Apr 6 15:02:19 EDT 2010


On 04/07/10 03:23, gerardob wrote:

> The error below appears. In the case i remove the comment to initialize m2,
> the same thing happens. Any ideas on how to fix this?
> 

When unpickling a user-defined class, you unpickling module must have
access to the original class definition. This means if you do this:

# model.py
class MyClass(object):
    pass

# saver.py

import pickle
import model

m = model.MyClass()
pickle.dump(m, open('...', 'w'))



Then the loader.py must be able to import model. If you do not
explicitly import model, pickle will automatically try to `import model`
from the standard module search path.

# loader.py

# the import must succeed, or pickle cannot find Foo's definition
#
# import model

pickle.load(open('...'))



More information about the Python-list mailing list