Title: cPickle - module object has no attribute
Type: Stage:
Components: None Versions: Python 2.5
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, ntroutman, rb
Priority: normal Keywords:

Created on 2009-03-18 17:08 by ntroutman, last changed 2009-12-05 18:51 by rb. This issue is now closed.

File name Uploaded Description Edit ntroutman, 2009-03-18 17:08 Creates a list of Foo objects and pickles them 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: which defines the class Foo and pickles out a list of objects 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") 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

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?
Date User Action Args
2009-12-05 18:51:50rbsetnosy: + rb
messages: + msg95998
2009-03-18 19:08:30benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg83770

resolution: rejected
2009-03-18 17:38:01ntroutmansetmessages: + msg83766
2009-03-18 17:08:42ntroutmansetfiles: +

messages: + msg83760
2009-03-18 17:08:11ntroutmancreate