importing class objects from a pickled file

Gregory Ewing greg.ewing at canterbury.ac.nz
Wed May 4 22:28:34 EDT 2011


Catherine Moroney wrote:

> 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
> 
> Even when I put statements into the code such as "from Y.X import X" ...
> the import statement works, but I am still unable to read the object

Is the program that reads the pickle file the same one that
was used to write it?

If not, what might be happening is that the writing program
has module X at the top level instead of inside package Y.
Then it will get pickled simply under the name "X" instead
of "Y.X", and the reading program will expect to find it at
the top level as well.

It's important that the reading and writing programs agree
about the location of the pickled classes in the package
namespace, unless you take steps to customise the pickling
process.

-- 
Greg



More information about the Python-list mailing list