No module named sklearn

Hi, the streamlit app is working in my local machine, but when I deploy it gives error like no module named sklearn in Prediction.py. I have entered scikit-learn in requirements.txt.Can u help please?
Github link: https://github.com/lalahashimli/customerChurn/blob/master/pages/Prediction.py
App: https://customerchurn-lc8whqvyzghwc7g2v48ley.streamlit.app/Prediction

File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)File "/mount/src/customerchurn/pages/Prediction.py", line 15, in <module>
    xgb_pipe = pickle.load(pickled_model)

what is the problem here?

Hi there @lalahashimli ,

Whenever you unpickle a model, remember to import the associated Python class beforehand. In your specific case, import the class of xgb_pipe before unpickling it.

Hope this helps!

Can u say in detailed, please? you mean I have to create class like in OOP? if yes , how can I do?
or I should import smth like from sklearnā€¦

Canā€™t test right now, but try importing sklearn at the top of the file. Your xgb_pipe object needs to recognize the Pipeline class when it is unpickled, so maybe try:

from sklearn.pipeline import Pipeline

Let me know if it works!

It didnā€™t work .Now gives such error ModuleNotFoundError: No module named ā€˜sklearnā€™

Traceback:

File "/home/adminuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)File "/mount/src/customerchurn/pages/Prediction.py", line 9, in <module>
    import sklearn

The versions of the some of packages like pandas ,numpy ,xgboost etc are different in streamlit cloud and in my jupyter notebook.Can it be the reason for this problem?

Hi again @lalahashimli ,

I just tested the Prediction.py page of your app in my local machine and it works. Make sure your requirements.txt has all the libraries and versions you need, so Streamlit Cloud takes them into account. Not specifying versions or specifying versions you havenā€™t tested is a common reason for errors.

It has been already solved. Thanks, again @marduk

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.