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

一曲冷凌霜 提交于 2020-07-18 20:19:07

问题


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

Running this function works fine But when I include this function in my Django Project I get the following error

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

The object being pickled is

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)

Im using the latest version of Django and Python3


回答1:


I had the same error - turns out I wasn't importing the class before trying to open it. The GUI needs to know how to construct the object before being able to read it. Try:

from YourModuleName import NamedEntityChunker

before calling your opening function.



来源:https://stackoverflow.com/questions/45483349/attributeerror-cant-get-attribute-on-module-main-from-manage-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!