pickle and module package

M.-A. Lemburg mal at lemburg.com
Mon May 17 16:59:36 EDT 1999


Jeffrey Kunce wrote:
> 
> 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.

As you already noted: the two modules could in fact be
two different libs and it is not just pickle that treats
them differently but also the sys.modules dictionary.

One thing I have stumbled into when starting off into the
package business was that:

from MyPackage import ThisModule
import ThisModule

yield *two* imports of the same module if done from a
module in the MyPackage directory -- the module is
initialized twice which causes a lot of trouble sometimes.

My solution was always using the 'from MyPackage import ThisModule'
notation -- even inside the package itself. To make this waterproof
you can add init code to the modules that checks whether they have
already been imported by e.g. placing an entry into a dictionary
sys.module_registry.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                            Y2000: 228 days left
Business:                                      http://www.lemburg.com/
Python Pages:                 http://starship.python.net/crew/lemburg/





More information about the Python-list mailing list