Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save objects in Python without using pickle?

I want to save a object in a file so i can use it later (after the program closes).

I tried to use pickle, but looks like it doesn't like my object :D

Traceback (most recent call last):
  File "(path)", line 4, in <module>
    pickle.dump(object, f)
AttributeError: Can't pickle local object '_createenviron.<locals>.encodekey'

here is the code:

import pickle
object = MyWeirdClass()
with open("data.pickle", "wb") as f:
    pickle.dump(object, f)

There is any other way to save objects (like an external library)? I did something wrong and i got this error? My MyWeirdClass() class is working perfectly fine, i tested it multiple times and i got exactly the results i expected.

EDIT:

i found out that the problem is that one of the object's variables is a selenium.webdriver.chrome.webdriver.WebDriver object. after deleting this object (after doing what i wanted with it) it worked fine.

SECOND EDIT:

i also got this error:

RecursionError: maximum recursion depth exceeded while pickling an object

On the line of code i tried to write the object to file.

I found out that i have to set the sys.setrecursionlimit() to a higher value, so instead of setting it to random values until i got no errors, i make this:

import pickle
import sys
default_cursion_limit = sys.getrecursionlimit()# defalut is 1000
object = MyWeirdClass()
while True:
    try:
        with open("data.pickle", "wb") as f:
            pickle.dump(object, f)
        break
    except RecursionError:
        default_cursion_limit += 50
        sys.setrecursionlimit(default_cursion_limit)# looks like its working with 2600

1 Answers

The easiest solution is going to be to define your class in such a way that it's pickle-able. The error message suggests that some of your class's attributes can't be pickled because they don't have globally-scoped names.

If you want to save an object that's not pickleable, you'll need to write your own logic to serialize and deserialize it. It's impossible to give specific advice on this without seeing the object, but the general idea is that you need to figure out how to represent your object's state as something that you CAN pickle (like a series of simple string/int attributes) and then write a function that will reconstitute your object from that data.

like image 95
Samwise Avatar answered Sep 22 '25 21:09

Samwise