importing class objects from a pickled file

James Mills prologic at shortcircuit.net.au
Tue May 3 22:29:53 EDT 2011


On Wed, May 4, 2011 at 10:18 AM, Catherine Moroney
<Catherine.M.Moroney at jpl.nasa.gov> wrote:
> 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.

pickled objects won't work this way unless you "customize" the
way in which your class gets serialized.

See: http://docs.python.org/library/pickle.html#the-pickle-protocol

Any time you want to unpickle a user class, that class must be available.

I suggest serializing to a more common format (say JSON) and re-create
your class with the data.

cheers
James

-- 
-- James Mills
--
-- "Problems are solved by method"



More information about the Python-list mailing list