Ashish Cherian
Ashish Cherian

Reputation: 387

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

Upvotes: 10

Views: 23713

Answers (2)

Pritam
Pritam

Reputation: 41

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

Upvotes: 4

bidby
bidby

Reputation: 738

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.

Upvotes: 9

Related Questions