1

I have an object that I'm trying to load using pickle (Iv'e tried JSON also and still not working, it says that it is not JSON serializable) and getting the following error:

Can't get attribute 'TextToSequence' on Code/voc-git/system/classifier_tools.py'>

Saving the object was really easy and worked perfectly, the issue rises just when I'm trying to load it.

Iv'e used the following code for the saving process:

with open(seq_obj_file, 'wb') as f:
        pickle.dump(seq, f, pickle.HIGHEST_PROTOCOL)

while seq is the object I'm saving to the file seq_obj_file

For loading Iv'e used the following code:

with open(seq_obj, 'rb') as pkl:
        self.input_vectorizer = pickle.load(pkl)

While self.input_vectorizer is an empty variable.

The object I'm trying to save belongs to the class TextToSequence.

I came across the solution proposed here, in my case I need to save the object inside a different class and can't export it to an outside main function.

Community
  • 1
  • 1
Lior Magen
  • 1,533
  • 2
  • 15
  • 33
  • Possible duplicate of [Unable to load files using pickle and multipile modules](http://stackoverflow.com/questions/27732354/unable-to-load-files-using-pickle-and-multipile-modules) – Roman Levin Nov 16 '15 at 12:41
  • There's no reason not to use json here. Why didn't it work for you? – Roman Levin Nov 16 '15 at 12:44
  • When trying to save object using json it returns an error which says that it is not JSON serializable. The solutions Iv'e found for using JSON doesn't work for my kind of object. – Lior Magen Nov 16 '15 at 14:41
  • If you can translate the object to strings, numbers, lists, and dicts - then you can teach `json` to serialize it. `pickle` is often a [bad idea](http://eev.ee/blog/2015/10/15/dont-use-pickle-use-camel/). – Roman Levin Nov 16 '15 at 14:45
  • Is it? Iv'e used pickle before and never had this issue. Still I want to use pickle and will continue to try to find a solution for it. – Lior Magen Nov 16 '15 at 14:57
  • It might fail when unpickling from a different version of Python or when the definition of the object changed since it was pickled. It's also a security risk since it can execute arbitrary code. Not to mention that you won't be able to use your data at all from a program not written in Python. – Roman Levin Nov 16 '15 at 15:01
  • Good point, I'll remember that. – Lior Magen Nov 16 '15 at 15:09

0 Answers0