2

I run into this problem when testing some code interactively on PyCharm 4.5.3 Community edition. Pickling class objects does not work in the interactive console. Here my minimal code:

import pickle

class Test:
    def __init__(self, name):
        self.name = name

with open('testdata.pkl', 'wb') as output:
    test1 = Test('Fritz')
    pickle.dump(test1, output, pickle.HIGHEST_PROTOCOL)

    test2 = Test('Hans')
    pickle.dump(test2, output, pickle.HIGHEST_PROTOCOL)

Running this on an interactive console in PyCharm 4.5.3 using Python 3.4.3 on OSX 10.10.4, I get the following error:

Traceback (most recent call last):
File "<input>", line 7, in <module> _pickle.PicklingError: 
Can't pickle <class 'Test'>: attribute lookup Test on builtins failed

The code works when executed with 'Run' in PyCharm. It's not a big thing but it cost me quite some time as I was not aware that there can be differences between running code in a 'fresh' interactive console, compared to execution using the 'Run' command. Could be the same issue as here

Community
  • 1
  • 1
Lorenz
  • 45
  • 1
  • 6
  • The line that's failing is trying to pickle a *class* (`Test`), not an *instance* (`test2`). Is that what you're trying to do? – Blorgbeard Aug 04 '15 at 04:00
  • I want to save the instance data 'Fritz', 'Hans' to retrieve it later again. – Lorenz Aug 04 '15 at 04:02
  • Well then `pickle.dump(Test, output, pickle.HIGHEST_PROTOCOL)` should be `pickle.dump(test2, output, pickle.HIGHEST_PROTOCOL)` – Blorgbeard Aug 04 '15 at 04:03
  • sorry that was a typo. Changed it to test2. But that doesn't change the error message. – Lorenz Aug 04 '15 at 04:07
  • Are you sure? Your code works for me with no errors, and saves a binary file in which I can make out the names "Hans" and "Fritz". – Blorgbeard Aug 04 '15 at 04:07
  • Related: http://stackoverflow.com/questions/4677012/python-cant-pickle-type-x-attribute-lookup-failed ? – Blorgbeard Aug 04 '15 at 04:10
  • Weird. I got this error running on a console in PyCharm 4.5.3. Running it from IDLE it works as you say. So it seems to be some other problem that I have. I'll close the issue. Thanks for the quick answer. – Lorenz Aug 04 '15 at 04:13
  • Oh ok. Seems there's a few similar questions involving PyCharm, maybe it does something to the environment that pickle doesn't like. – Blorgbeard Aug 04 '15 at 04:16
  • I tested your code in PyCharm 4.0.4 and it works fine. – f.rodrigues Aug 04 '15 at 04:36
  • @f.rodrigues did you execute with 'Run' or in an interactive console. The problem seems to be just in the interactive console. – Lorenz Aug 04 '15 at 04:45
  • Either of those worked here. – f.rodrigues Aug 04 '15 at 05:49
  • Nov 2021, and the problem is still there! For 2 years, I was developing mechanisms to get around it, and now I realize there is nothing wrong with Pickle or Dill modules, it is the crappy console!! my life is a lie. – Alex Deft Nov 22 '21 at 06:05
  • I have the same problem (I think). When I run the model using the terminal `python thescript.py`, but when I run it using the play-button or in the console it gives this pickle error. (in my case when a toolbox that I uses tries to save some system) – seaver Feb 18 '22 at 17:02

0 Answers0