AttributeError: Can't get attribute on <module '__main__' from 'manage.py'>

AttributeError: Can't get attribute on <module '__main__' from 'manage.py'>

def getNer(text):
    with open('chunker.pkl', 'rb') as pickle_file:
        chunker = pickle.load(pickle_file)
    return chunker.parse(pos_tag(word_tokenize(text)))

运行 这个函数工作正常 但是当我在我的 Django 项目中包含这个函数时 我收到以下错误

chunker = pickle.load(pickle_file)
AttributeError: Can't get attribute 'NamedEntityChunker' on <module '__main__' from 'manage.py'>

被 pickle 的对象是

class NamedEntityChunker(ChunkParserI):
    def __init__(self, train_sents, **kwargs):
        assert isinstance(train_sents, Iterable)

        self.feature_detector = features
        self.tagger = ClassifierBasedTagger(
            train=train_sents,
            feature_detector=features,
            **kwargs)

    def parse(self, tagged_sent):
        chunks = self.tagger.tag(tagged_sent)
        iob_triplets = [(w, t, c) for ((w, t), c) in chunks]
        return conlltags2tree(iob_triplets)

我正在使用最新版本的 Django 和 Python3

我遇到了同样的错误 - 原来我在尝试打开 class 之前没有导入它。 GUI 在能够读取对象之前需要知道如何构造对象。尝试:

from YourModuleName import NamedEntityChunker

在调用打开函数之前。

我在 Django views.py 文件中加载腌制的 ML 模型(从头开始实现)时遇到了同样的问题。我通过在调用 main 方法之前,即在开始我们的项目之前,在 manage.py 文件中导入在 ML 模型中保存或腌制的函数来解决错误。

例如:

if __name__ == '__main__':

    from ml_model.nb_model import NB
    main()