[IPython-user] can't pickle interactively defined function

Robert Kern robert.kern@gmail....
Mon Oct 27 13:04:43 CDT 2008

Robin wrote:
> On Mon, Oct 27, 2008 at 5:19 PM, Robert Kern <robert.kern@gmail.com> wrote
>> We're not putting things defined in the namespace in the __main__ module. I'm
>> not sure what the best approach should be, here. There might be multiple
>> namespaces floating around. Presumably, they shouldn't all be linked to __main__.
> Thanks for looking into it...
> I'm a bit out of my depth with this stuff - from Googling the __main__
> module in Ipython is actually a FakeModule - whether or not this is
> designed to contain interactively designed stuff I'm not sure.

Given this part of the docstring:

     This is not meant to replace a module, but to allow inserting a fake
     module in sys.modules so that systems which rely on run-time module
     importing (like shelve and pickle) work correctly in interactive IPython

it looks like it was meant for this purpose. Devs, has something changed or did 
pickling functions defined interactively never work? I notice that I get the 
same error when trying to pickle an instance of a class defined interactively:

In [5]: import cPickle

In [6]: class A(object):
    ...:     pass

In [7]: a = A()

In [8]: cPickle.dumps(a, 2)
PicklingError                             Traceback (most recent call last)

/Users/rkern/<ipython console> in <module>()

PicklingError: Can't pickle <class '__main__.A'>: attribute lookup __main__.A failed

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