pickle and module package

Benjamin Derstine bend at realeducation.com
Thu May 20 10:48:59 EDT 1999


Jeffrey Kunce wrote in message ...
Python is very flexible about importing modules from
different locations. For example:

    try: from MyPackage.MySubPackage import MyModule
    except ImportError: import MyModule
    MyInstance = MyModule.MyClass()
    ...

will first try to import MyModule from a specific package. If
that fails, python will attempt to import MyModule from anywhere
on thes pythonpath. Regardless of where MyModule comes
from, MyInstance will behave the same for any following code.

*Except* for the pickle module. pickle (and cPickle) store each
class instance with the full package-class-name. As far as pickle is
concerned:
    MyPackage.MySubPackage.MyModule.MyClass
and
    MyModule.MyClass
are completely different animals. When you unpickle
these instances, you are required to have imported the
modules from the same location as when you pickled them.

Is this a problem for anyone besides me? Does anyone have
an easy workaround?  Thanks.

  --Jeff

I've been running into this problem while using Zope.  I wanted to run
Python code through Zope that unpickled some classes.  For the sake of Zope
the modules have to be contained in packages.  When I would run the code
from the Python interpreter without the packaged version of the code it
worked fine. Then when I would try to run it from Zope everything worked
fine until it got to my unpickle statement.  I had no idea what was going on
until I saw your post.  I have no idea how to get around this except to
pickle and unpickle in the same place.

Ben







More information about the Python-list mailing list