Can't import my utility module

I am using a sklearn.externals.joblib

classifier to save the model to disk, which is actually using the module pickle

at a lower level.

I create my own CountVectorizer

class named StemmedCountVectorizer

and saved it in util.py

and then used it in a script to save the model

import util

from sklearn.externals import joblib

vect = util.StemmedCountVectorizer(stop_words='english', ngram_range=(1,1))

bow = vect.fit_transform(sentences)

joblib.dump(vect, 'vect.pkl') 

      

This is my project structure using Flask:

   |- sentiment/
     |- run.py
     |- my_app/
       |- analytic/
         |- views.py
         |- util. py
         |- vect.pkl

      

I run the application with python run.py

and try to load the saved object using joblib.load

in views.py

, but it doesn't work, I imported the module util

, but I get the error:

ImportError: No module named util

      

can anyone give a solution? thank

+3


source to share


1 answer


Looks like a problem with package / pythonpath. The system needs to know where to localize your modules. Do you have __init.py__

a folder my_app

and analytic

? File file __init__.py

directories on disk as Python package directories. And the structure should be like this

   |- sentiment/
     |- run.py
     |- my_app/
       |- __init__.py
       |- analytic/
         |- __init__.py
         |- views.py
         |- util. py
         |- vect.pkl

      

then in run.py

try importing with

import my_app.analytic.utils

      



or

from my_app.analytic.utils import <yourClassName>

      

for python package details check here . And be aware of the namespace issue.

+2


source







All Articles