📌  相关文章
📜  AttributeError:无法获取属性“”<module '__main__'> pickle - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:39.187000             🧑  作者: Mango

代码示例1
# Import the class that pickled the file in question into the object
# that does the unpickling

# BEFORE:
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)
  
  
if __name__ == '__main__':
    doc = Document()
    utils.save_document(doc)
    
    
# WORKING SOLUTION:
if __name__ == '__main__':
    from class_def import Document 
    # ^ so that it is using the Document class defined under the class_def module
    doc = Document()
    utils.save_document(doc)