将腌制的对象加载到其他文件中-属性错误 [英] Load pickled object in different file - Attribute error
问题描述
我在将腌制的文件加载到与腌制该文件的模块不同的模块中遇到了一些麻烦.我知道以下线程:无法使用pickle加载文件和多桩模块.我尝试了将类导入到我解开文件的模块中的建议解决方案,但是它一直给我同样的错误:
AttributeError: Can't get attribute 'Document' on <module '__main__' from ''>
I have some trouble with loading a pickled file in a module that is different from the module where I pickled the file. I am aware of the following thread: Unable to load files using pickle and multipile modules. I've tried the proposed solution of importing the class into the module where I am unpickling my file, but it keeps giving me the same error:
AttributeError: Can't get attribute 'Document' on <module '__main__' from ''>
我正在尝试做的基本结构:
The basic structure of what I am trying to do:
用来对对象进行腌制和去污的Util文件utils.py:
Util file that pickles and unpickles 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)
定义了Document对象并调用save util方法的文件class_def.py:
File where Document object is defined and the save util method is called, class_def.py:
import utils
class Document(object):
data = ""
if __name__ == '__main__':
doc = Document()
utils.save_document(doc)
调用load util方法的文件process.py:
File where the load util method is called, process.py:
import utils
if __name__ == '__main__':
utils.load_document(file_path)
运行process.py给出了提到的AttributeError.如果我将class_def.py文件导入process.py并按照原始线程中的说明运行其主要方法,则它可以工作,但是我希望能够分别运行这两个模块,因为class_def文件是一个预处理步骤,需要花费相当多的时间一段时间我该怎么解决?
Running process.py gives the mentioned AttributeError. If I import the class_def.py file into process.py and run its main method as mentioned in the original thread 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 some time. How could I solve this?
推荐答案
在您的class_def.py
文件中,您具有以下代码:
in your class_def.py
file you have this code:
if __name__ == '__main__':
doc = Document()
utils.save_document(doc)
这意味着doc
将是一个__main__.Document
对象,因此,对其进行腌制时,它希望能够从主模块中获取一个Document
类,要解决此问题,您需要使用的定义Document
来自名为class_def
的模块,这意味着您将在此处添加导入:
This means that doc
will be a __main__.Document
object, so when it is pickled it is expecting to be able to get a Document
class from the main module, to fix this you need to use the definition of Document
from a module called class_def
meaning you would add an import here:
(通常,您可以直接在if __name__ == "__main__"
内部执行from <own module name> import *
)
(in general you can just do from <own module name> import *
right inside the if __name__ == "__main__"
)
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)
那样,它将需要两次运行class_def.py文件,一次以__main__
身份运行,一次以class_def
身份运行,但这确实意味着数据将被腌制为class_def.Document
对象,因此加载该数据将检索从正确的地方上课.否则,如果您有一种从另一个对象构造一个文档对象的方法,则可以在utils.py
中执行类似的操作:
that way it will need to run the class_def.py file twice, once as __main__
and once as class_def
but it does mean that the data will be pickled as a class_def.Document
object so loading it will retrieve the class from the correct place. Otherwise if you have a way of constructing one document object from another you can do something like this in utils.py
:
def save_document(doc):
if doc.__class__.__module__ == "__main__":
from class_def import Document #get the class from the reference-able module
doc = Document(doc) #convert it to the class we are able to use
write_file = open(file_path, 'wb')
pickle.dump(doc, write_file)
尽管通常我更喜欢第一种方式.
Although usually I'd prefer the first way.
这篇关于将腌制的对象加载到其他文件中-属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!