importing class objects from a pickled file

Catherine Moroney Catherine.M.Moroney at jpl.nasa.gov
Tue May 3 20:18:33 EDT 2011


Hello,

I have an object of class X that I am writing to a pickled file.  The 
pickling part goes fine, but I am having some problems reading the 
object back out, as I get complaints about "unable to import module X".

The only way I have found around it is to run the read-file code out of 
the same directory that contains the X.py file, but this is obviously 
not a portable way of doing things.

Even when I put statements into the code such as "from Y.X import X"
where Y is the name of the python package that contains the X,py file,
the import statement works, but I am still unable to read the object 
from the pickled file, running into the same "unable to import module X"
error.

Am I explaining myself properly?  Why doesn't the code that loads the
object from the pickled file work unless I am sitting in the same 
directory?   The code that writes the pickled file has the statement
"from Y.X import X" statement" at the top, as does the reading code, but
even though that import statement succeeds, the read still fails with 
the import error.

Thanks for any help,

Catherine



More information about the Python-list mailing list