cPickle vs pickle discrepancy

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jan 6 19:57:43 EST 2009


En Mon, 05 Jan 2009 23:04:30 -0200, Zac Burns <zac256 at gmail.com> escribió:

> I have a module that attempts to pickle classes defined in that module.
>
> I get an error of the form:
> PicklingError: Can't pickle <class 'Module.SubModule.Class'>: import
> of module Module.SubModule failed
> when using cPickle (protocol -1, python version 2.5.1).
>
> The module has already been imported and is in sys.modules when the
> exception is raised.

There is no thing as a "submodule"; very likely you have a package, and a  
module inside that package.
- always import the "submodules" from the package, not directly (relative  
imports are safer)
- in general, don't play with PYTHONPATH, sys.path, and such things.
- don't directly execute a module inside a package (it's name is always  
__main__ and it doesn't even "know" it's part of a package).

> Using pickle instead of cPickle works, but the section of the code is
> performance critical.

Could you provide a short example? I'd say that if you stick to the above  
rules you won't have the issue again, but anyway I'd like to know in which  
cases Pickle and cPickle differ.

-- 
Gabriel Genellina




More information about the Python-list mailing list