tags:

views:

148

answers:

1

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
That's exactly what popped into my head.
wisty
+1. This is the only way (referencing modules) I've seen this happen (as in `import os,cPickle;cPickle.dumps(os.path)`)
ChristopheD
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
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
cool, that makes sense, thanks!
adum