Issue 5509: cPickle - module object has no attribute (original) (raw)

Created on 2009-03-18 17:08 by ntroutman, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Foo.py ntroutman,2009-03-18 17:08 Creates a list of Foo objects and pickles them
LoadFoo.py ntroutman,2009-03-18 17:08 Loads the pickled list
Messages (5)
msg83759 - (view) Author: Nathaniel Troutman (ntroutman) Date: 2009-03-18 17:08
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"
msg83760 - (view) Author: Nathaniel Troutman (ntroutman) Date: 2009-03-18 17:08
Why can I only attach one file at a time?
msg83766 - (view) Author: Nathaniel Troutman (ntroutman) Date: 2009-03-18 17:38
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.
msg83770 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-03-18 19:08
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.
msg95998 - (view) Author: (rb) * Date: 2009-12-05 18:51
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?
History
Date User Action Args
2022-04-11 14:56:46 admin set github: 49759
2009-12-05 18:51:50 rb set nosy: + rbmessages: +
2009-03-18 19:08:30 benjamin.peterson set status: open -> closednosy: + benjamin.petersonmessages: + resolution: rejected
2009-03-18 17:38:01 ntroutman set messages: +
2009-03-18 17:08:42 ntroutman set files: + LoadFoo.pymessages: +
2009-03-18 17:08:11 ntroutman create