0

I have 2 modules, because in pickle you have to load the pickles from a module called anything other than main as stated here:

However, pickle is throwing errors such as those here when i try to:

  • a() save a class from module 1 using module
  • b() Loading back the same class from module 1

If i run a and b in the same program, everything goes fine. When I do separately, the import of the class from module 1 to module 2 shows that the class does not exist

for example

#MODULE 2;
from module1 import class1
... some functions that change class1
a() #to save class1

Now I try to load back the saved class:

b() #Loading back the same class from module 1
print(class1)

To my surprise, class1 is the default definition of class1 and not the loaded.

Please, could you tell me why? How can I change to load it correctly? Maybe with any other module that works better than pickle?

Also, when I run the first module alone, something weird happens: I do not call any funtion or class, and surprisingly it prints as if one of my funtions, let me show you this piece of code which you can run and the output:

import pickle
from pprint import pprint

def cargado3():
    try:
        with open('somefile.pickle','rb') as f:  
            print('-af-')
            pprint(pickle.load(f))
    except:
        pass

cargado2=cargado3()

Output:

-af-
-af-
<class 'module1.class1'>

Thank you in advance

0 Answers0