I have some problems loading the pickled file in a module, which is different from the module where I pickled the file. I am aware of the following stream: Unable to upload files using pickle and multi-component modules . I tried the proposed solution for importing a class into a module, where I print my file, but it continues to give me the same error: AttributeError: Can't get attribute 'Document' on <module '__main__' from ''>
The basic structure of what I'm trying to do is:
A utility file that parses and unpacks objects, utils.py:
import pickle def save_document(doc): from class_def import Document write_file = open(file_path, 'wb') pickle.dump(doc, write_file) def load_document(file_path): from class_def import Document doc_file = open(file_path, 'rb') return pickle.load(doc_file)
The file in which the Document object is set and the save utility method, class_def.py is called:
import utils class Document(object): data = "" if __name__ == '__main__': doc = Document() utils.save_document(doc)
The file where the load loading method is called, process.py:
import utils if __name__ == '__main__': utils.load_document(file_path)
Running process.py gives the mentioned AttributeError attribute. If I import the class_def.py file into process.py and run its main method as indicated in the source stream, it works, but I want to be able to run these two modules separately, since the class_def file is a preprocessing step that takes quite when something. How can i solve this?
blackplant
source share