[spyder] Difficulty pickling objects

416 views
Skip to first unread message

Gavin

unread,
May 8, 2010, 11:27:18 PM5/8/10
to spyder
I'd like to pickle an object that is essentially a dictionary, but get
a pickling error if I run it in the interactive console but not the
external console, or iPython.

The error:
Traceback (most recent call last):
File "C:\neph\proc\src\testpkl.py", line 23, in <module>
pk1.dump(j1)
PicklingError: Can't pickle <class '__main__.Person'>: attribute
lookup __main__.Person failed

An example script:
import cPickle as pickle
class Person(object):
def __init__(self):
self.name = 'Jesus'
self.character = 'Naughty Boy'
j1 = Person()
print('In')
print(j1.name)
print(j1.character)
p1 = 'met.pkl'
with open (p1, 'wb') as fh:
pk1 = pickle.Pickler(fh, -1)
pk1.dump(j1)
with open(p1) as fh:
pk2 = pickle.Unpickler(fh)
out = pk2.load()
print('Out')
print(out.name)
print(out.character)

I found a similar post here:
http://www.pubbs.net/200904/python/31050-cpickle-and-subclassing-lists.html
which pointed to a problem with iPython, which appears to have been
fixed.

Ultimately I can run the scripts outside Spyder but I'd prefer to test
this inside.
Thanks, Gavin.

--
You received this message because you are subscribed to the Google Groups "spyder" group.
To post to this group, send email to spyd...@googlegroups.com.
To unsubscribe from this group, send email to spyderlib+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spyderlib?hl=en.

Pierre Raybaut

unread,
May 26, 2010, 6:12:26 AM5/26/10
to spyder
Hi,

Sorry for the late reply.

Apparently this error is related to the fact that Python is not
founding an attribute because it has an old version of your class in
memory (reimporting it won't help unless you do a 'reload').
I guess (and I'm quite sure about it) that enabling the feature 'Force
modules to be completely reloaded' (menu "Interactive console",
submenu "Interactive console settings") should do the trick: it will
force the Python interpreter to reload completely your class each time
you are executing your script in the interactive console.

Thanks for the link about IPython bug report, it helped me to
understand your problem very quickly.

Cheers,
Pierre

> I found a similar post here:http://www.pubbs.net/200904/python/31050-cpickle-and-subclassing-list...

Reply all
Reply to author
Forward
0 new messages