pickling extension class

Alex Martelli aleaxit at yahoo.com
Tue Jan 18 15:51:40 EST 2005


harold fellermann <harold.fellermann at upf.edu> wrote:
   ...
> Here it goes...:
>   OOPS, error (exceptions.ImportError): No module named hyper

So, the __import__ in pickle fails -- indeed, __import__('foo') when
'foo' ``is imported from a subpackage'' is _supposed_ to fail, as
'hyper' is not the name you SHOULD be importing.  For example,

__import__('email.Encoders')

is fine, but just

__import__('Encoders')

fails with ImportError -- there IS no toplevel module by that name!


> I have noticed that the error does not occur, when the imported module
> ('hyper') is in the same directory as the script that pickles. When it
> is imported from a subpackage (like in the code
> I sent you) it goes wrong.

If you can't fix the modulename given by your type, you can perhaps
kludge things up by (e.g.)

import the.real.hyper
sys.modules['hyper']=the.real.hyper

before you pickle; but I suspect UNpickling would fail in that case.

Using the standard library copy_reg module to register your way to
pickle and recover instances of your type might work better.


Alex



More information about the Python-list mailing list