1

I have written a class that I need to be able to pickle and unpickle using python's pickle module. Pickling seems to work OK, but when I unpickle I get an error. I can replicate the error using the simpler class given below:

class TestClass(object):
    def __init__(self):
        self.teststr = 'Hello'
    def __str__(self):
        return self.teststr
    def __repr__(self):
        return self.teststr
    def test(self):
        return 'test: {0}'.format(self.teststr)

If I run the following code in python session, everything works fine:

import pickle as pi

mytest = TestClass()
fs ='C:/Desktop/pickle'
f = open(fs, 'w')
pi.dump(mytest, f)
f.close()

f = open(fs, 'r')
unpi = pi.load(f)
f.close()

print unpi

But if I restart python, and run the following code:

import pickle as pi
fs ='C:/Desktop/pickle'

f = open(fs, 'r')
unpi = pi.load(f)

I get the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Programs\Coding\Languages\Python\Anaconda_32bit\Conda\lib\pickle.py",
 line 1378, in load
    return Unpickler(file).load()
  File "C:\Programs\Coding\Languages\Python\Anaconda_32bit\Conda\lib\pickle.py",
 line 858, in load
    dispatch[key](self)
  File "C:\Programs\Coding\Languages\Python\Anaconda_32bit\Conda\lib\pickle.py",
 line 1090, in load_global
    klass = self.find_class(module, name)
  File "C:\Programs\Coding\Languages\Python\Anaconda_32bit\Conda\lib\pickle.py",
 line 1126, in find_class
    klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'TestClass'

If I then re-define the TestClass object in the new python session and re-run the second block of code, I get a different error, so thats no help.

I realize this question looks like: link

But I don't think it is, as the accepted answer seems to suggest that attributes defined as self.a (as opposed to just a) will allow pickle to work properly?

I actually need to be able to use my class with Apache Spark, which seems to only work with objects that are pickle-able.

Community
  • 1
  • 1
user3279453
  • 403
  • 1
  • 3
  • 12
  • 1
    Pickle can only work if the class you are pickling can be imported *from the same module each time*. You need to put `TestClass` in a separate module that can be imported when unpickling. – Martijn Pieters Mar 19 '15 at 14:50

0 Answers0