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
MyWeirdClass()?