cPickle vs pickle discrepancy

Zac Burns zac256 at gmail.com
Thu Jan 8 19:27:45 CET 2009

Thanks for your patience waiting for me to isolate the problem.

| Package
--__init__.py ->empty
--Package.py ->empty
     import cPickle
     class C(object):
     def fail():
        return cPickle.dumps(C(), -1)

import Package.Module

The failure seems to happen because pickle correctly does an absolute
import and cPickle incorrectly relatively imports Package.py and fails
to find Module underneath.

The package and the module named package was because in there was a
main class defined with the same name as the package in that file and
Module.py contained support code for the package.


Zachary Burns
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games

On Tue, Jan 6, 2009 at 4:57 PM, Gabriel Genellina
<gagsl-py2 at yahoo.com.ar> wrote:
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list

More information about the Python-list mailing list