Forums

loaded_model = pickle.load(fs)#012ModuleNotFoundError: No module named 'DocToWord'

I am not able to load the machine learning existing model while hitting through a flask api.

class DocToWord:
    def __init__(self, userDoc):
        self.userDoc = userDoc
    def getWordWithoutTopics(self):
        #SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
        SITE_ROOT = os.path.dirname(__file__)
        logging.info(SITE_ROOT)
        filename = os.path.join(SITE_ROOT,'docToWord_WithoutTopics_Model.sav')
        logging.info(filename)
        with open(filename, 'rb') as fs:
            loaded_model = pickle.load(fs)

            prediction = loaded_model.predict([self.userDoc])
            return str(prediction)

Getting the below error in errors.log file:

Exception on /api/v1/findProfession [POST]
Traceback (most recent call last):
  File "/home/heliumwavedoc/.virtualenvs/env_natural_api/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/heliumwavedoc/.virtualenvs/env_natural_api/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/heliumwavedoc/.virtualenvs/env_natural_api/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/heliumwavedoc/.virtualenvs/env_natural_api/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/heliumwavedoc/.virtualenvs/env_natural_api/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
   rv = self.dispatch_request()
  File "/home/heliumwavedoc/.virtualenvs/env_natural_api/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/heliumwavedoc/google_natural/app/api/views.py", line 78, in findProfession
    prediction = docToWordObj.getWordWithoutTopics()
  File "/home/heliumwavedoc/google_natural/app/api/DocToWord.py", line 78, in getWordWithoutTopics
    loaded_model = pickle.load(fs)
ModuleNotFoundError: No module named 'DocToWord'

[edit by admin: formatting]

I think that the problem is that the code that pickled the object was using a different import path to the code that is unpickling it. This Stack Overflow answer explains the problem.

@giles Thanks for the response. It is working now. Added the code below before loading the pickle file.

import sys
sys.path.append(r'path/to/python module file')
with open(filename, 'rb') as fs:
            loaded_model = pickle.load(fs)

            prediction = loaded_model.predict([self.userDoc])
            return str(prediction)

Excellent -- thanks for posting the details!