Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 606
Ashish Cherian Avatar asked Aug 03 '17 11:08

Ashish Cherian


2 Answers

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.

like image 177
bidby Avatar answered Nov 10 '22 15:11

bidby


I had the same problem while loading pickled ML model (which was implemented from scratch) in Django views.py file. And i solved the error by importing the function that was saved or pickled in the ML model in manage.py file just before calling main method i.e. just before starting our project.

for example:

if __name__ == '__main__':

    from ml_model.nb_model import NB
    main()
like image 4
Pritam Avatar answered Nov 10 '22 15:11

Pritam