pickle and module package

Jeffrey Kunce kuncej at mail.conservation.state.mo.us
Mon May 17 14:26:03 EDT 1999


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
    





More information about the Python-list mailing list