If I define a class Foo in module A and in module A pickle out a list of Foo objects to 'foo.pkl', then in module B attempt to unpickle 'foo.pkl' I recieve the error "AttributeError: 'module' object has no attribute 'Foo'" Attached are: Foo.py which defines the class Foo and pickles out a list of objects LoadFoo.py attempts to load the list of objects pickled by Foo I'm running Vista with "Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32"
I believe I've tracked down the problem. When you run a python module directly (ie "python Foo.py") any classes defined in the module have their '__module__' attribute set to '__main__'. Which means the pickle says the class is in '__main__' of whatever module is trying to load the file. I think it would make more sense to actually include the module name, this means that an external module need simply ensure that the pickled class's module be imported with the correct name.
Implementing this may be difficult (the module may not be on the search path when its run and there's no way to determine that). Also, it would break compatibility. Anyway, it's usually better to define classes that will be pickled in their own permanent module.
I've just been stung by this. I've noticed that this seems to apply to both cPickle and pickle. Even worse, it causes different behaviour when a program is run under pdb because __main__ is suddenly pdb rather than the program itself. So, in summary, neither pickle nor cPickle can pickle a class if it is not defined in its own module? Surely this is an obvious deficiency of pickle and if it is not going to be fixed it should at least be documented as such?