pickling question

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Sep 2 13:14:32 EDT 2009


On 02:06 pm, garyrob at me.com wrote:
>When you define a class in a script, and then pickle instances of that 
>class in the same script and store them to disk, you can't load that 
>pickle in another script. At least not the straightforward way 
>[pickle.load(file('somefile.pickle'))]. If you try it, you get an 
>AttributeError during the unpickling operation.
>
>There is no problem, of course, if the class is defined in a module 
>which is imported by the pickling script. 
>pickle.load(file('somefile.pickle')) then works.
>
>Rather than provide specific examples here, there's a blog post from 
>2005 that discusses this issue in depth and presents the problem very 
>well: http://stefaanlippens.net/pickleproblem. (I tested in Python 2.6 
>yesterday and the same issue persists.)
>
>Questions:
>
>1) Does this have to be the case, or is it a design problem with 
>pickles that should be remedied?
>
>2) Is there an easier way around it than moving the class definition to 
>a separate module? The blog post I point to above suggests putting 
>"__module__ = os.path.splitext(os.path.basename(__file__))[0]" into the 
>class definiton, but that's not working in my testing because when I do 
>that, the pickling operation fails. Is there something else that can be 
>done?

I described another solution here: 
http://jcalderone.livejournal.com/45604.html

Jean-Paul



More information about the Python-list mailing list