i'm trying to pickle a big class and getting "TypeError: can't pickle module objects". despite looking around the web, i can't exactly figure out what this means. and i'm not sure which "module object" is causing the trouble. is there a way to find the culprit? the stack trace doesn't seem to indicate anything.
+4
A:
I can reproduce the error message this way:
import cPickle
class Foo(object):
def __init__(self):
self.mod=cPickle
foo=Foo()
with file('/tmp/test.out', 'w') as f:
cPickle.dump(foo, f)
# TypeError: can't pickle module objects
Do you have a class attribute that references a module?
unutbu
2010-05-07 18:41:18
That's exactly what popped into my head.
wisty
2010-05-07 18:49:59
+1. This is the only way (referencing modules) I've seen this happen (as in `import os,cPickle;cPickle.dumps(os.path)`)
ChristopheD
2010-05-07 18:50:41
that make sense, but i'm not sure how to find it. (this class i didn't write myself, and it's 3500 lines long.) any idea how i might locate the reference? thanks!
adum
2010-05-07 19:22:56
The only thing that springs to mind is recursive descent.. do a dir(...) on the object, and try to pickle each of the attributes separately. Take the one the gives the error, and repeat same until you found the module object.
wump
2010-05-07 19:50:19
cool, that makes sense, thanks!
adum
2010-05-07 20:08:34