AttributeError:无法获取<module'的属性__main __'=“”from =“”'manage.py'=“”>

时间:2017-08-03 11:50:12

标签: python django python-3.6

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'>

被腌制的对象是

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

1 个答案:

答案 0 :(得分:0)

我有同样的错误-事实证明,在尝试打开该类之前,我没有导入该类。 GUI必须先知道如何构造对象才能读取它。试试:

from YourModuleName import NamedEntityChunker

在调用打开功能之前。