[IPython-user] cPickle works in standard interpreter, but not in IPython

Robert Kern robert.kern@gmail....
Fri Apr 17 14:50:27 CDT 2009


On 2009-04-17 13:09, Reckoner wrote:
> for example:
>
> In IPython:
>
>
>>> class Mylist(list):
>          def __init__(self,x=[]):
>              list.__init__(self,x)
>
>>> from cPickle import dumps
>>> w=Mylist([1,2,3])
>>> dumps(w)
>
> PicklingError: Can't pickle<class '__main__.Mylist'>: attribute
> lookup __main__.Mylist failed
>
> However, using the standard Python interpreter:
>
> Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from cPickle import dumps
>>>> class Mylist(list):
> ...         def __init__(self,x=[]):
> ...             list.__init__(self,x)
> ...
>>>> w=Mylist([1,2,3])
>>>> dumps(w)
> 'ccopy_reg\n_reconstructor\np1\n(c__main__\nMylist\np2\nc__builtin__\nlist\np3\n
> (lp4\nI1\naI2\naI3\natRp5\n.'
>
>
>
> Why is this?

The __main__ module is faked in IPython. You cannot pickle instances of classes 
which were defined in the interpreter itself. This is an area that can be 
improved, but it is quite tricky.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco



More information about the IPython-user mailing list