I have sklearn version 0.24.1 but it is showing me this error in my streamlit app....don't know how to solve this

ModuleNotFoundError: No module named 'sklearn.linear_model._logistic'
Traceback:
File "c:\users\chintan\appdata\local\programs\python\python37\lib\site-packages\streamlit\script_runner.py", line 332, in _run_script
    exec(code, module.__dict__)
File "C:\Users\chintan\Desktop\streamlit\Final_year_project\App.py", line 317, in <module>
    main()
File "C:\Users\chintan\Desktop\streamlit\Final_year_project\App.py", line 252, in main
    loaded_model = load_model("logistic_regression_model.pkl")
File "C:\Users\chintan\Desktop\streamlit\Final_year_project\App.py", line 84, in load_model
    loaded_model = joblib.load(open(os.path.join(model_file),"rb"))
File "c:\users\chintan\appdata\local\programs\python\python37\lib\site-packages\joblib\numpy_pickle.py", line 575, in load
    obj = _unpickle(fobj)
File "c:\users\chintan\appdata\local\programs\python\python37\lib\site-packages\joblib\numpy_pickle.py", line 504, in _unpickle
    obj = unpickler.load()
File "c:\users\chintan\appdata\local\programs\python\python37\lib\pickle.py", line 1088, in load
    dispatch[key[0]](self)
File "c:\users\chintan\appdata\local\programs\python\python37\lib\pickle.py", line 1376, in load_global
    klass = self.find_class(module, name)
File "c:\users\chintan\appdata\local\programs\python\python37\lib\pickle.py", line 1426, in find_class
    __import__(module, level=0)

Hi @Ritvik07Mahajan, welcome to the Streamlit community!

Can you post a repo with your code so that we can take a look?

Best,
Randy

okay sir…

here is the github link:

Hi @Ritvik07Mahajan, thanks for sharing your code! :grinning:

It took me a while to figure out what may be causing the error. From the notebook in your repo, I see scikit-learn==0.24.1 is installed in a conda environment. You may be getting this error because you’re running the app outside of the conda environment where scikit-learn is installed. Could you try either:

  1. Running the app within the conda environment or
  2. Installing scikit-learn==0.24.1 globally

Hopefully, the error is caused by this environment mismatch and nothing more obscure. Let me know if any of the options :point_up: work or you need more help debugging.

Best,
Snehan :balloon:

1 Like

Maybe I’m wrong, but that’s how I see it:
You load a model or an object, which is unpacked with “unpickle”.
But nowhere in the App.py you import the associated modules from sklearn.
Your App.py therefore does not know the type of this loaded object.

@Franky1 I suspected exactly that initially as well :thinking: However, I was able to clone the repo and successfully run App.py as-is on my machine, without having to import sklearn or any of its estimators in the script.

@snehankekre
Okay, I’ve learned something new. :wink:
I would not have thought that it would work.

1 Like

can you plz tell me how to install it globally?

If you’re not using virtual environments and using pip as your dependency manager:

 pip install scikit-learn

It is showing me this

C:\Users\chintan>pip install scikit-learn
Requirement already satisfied: scikit-learn in c:\python39\lib\site-packages (0.24.1)
Requirement already satisfied: scipy>=0.19.1 in c:\python39\lib\site-packages (from scikit-learn) (1.6.2)
Requirement already satisfied: threadpoolctl>=2.0.0 in c:\python39\lib\site-packages (from scikit-learn) (2.1.0)
Requirement already satisfied: joblib>=0.11 in c:\python39\lib\site-packages (from scikit-learn) (1.0.1)
Requirement already satisfied: numpy>=1.13.3 in c:\python39\lib\site-packages (from scikit-learn) (1.20.2)
WARNING: You are using pip version 20.2.3; however, version 21.1.2 is available.
You should consider upgrading via the ‘c:\python39\python.exe -m pip install --upgrade pip’ command.

It looks like scikit-learn is already installed! :slight_smile: If you similarly have your other Python dependencies installed, you can run your app from the command prompt by passing the path of your App.py file to streamlit run. For example:

streamlit run C:\Users\chintan\Desktop\streamlit\Final_year_project\App.py

Best, :balloon:
Snehan

sir, it is showing me same error :frowning:

Thanks for sharing! It looks like you have two different versions of Python installed. scikit-learn was installed with pip using Python 3.9 and streamlit run uses Python 3.7.

Could you try:

c:\python39\python.exe -m streamlit run C:\Users\chintan\Desktop\streamlit\Final_year_project\App.py

Let me know if this works :point_up: or you’re running into other errors.

Best, :balloon:
Snehan

c:\python39\python.exe: No module named streamlit

Ok, we’re almost there! :grinning_face_with_smiling_eyes: All you now have to do is pip install the Python modules used in your App.py file. For example:

c:\python39\python.exe -m pip install streamlit Pillow pandas matplotlib

Install any other Python dependencies that way, and then run:

c:\python39\python.exe -m streamlit run C:\Users\chintan\Desktop\streamlit\Final_year_project\App.py

Best, :balloon:
Snehan

WARNING: Failed to write executable - trying to use .deleteme logic
ERROR: Could not install packages due to an EnvironmentError: [WinError 2] The system cannot find the file specified: ‘c:\python39\Scripts\pygmentize.exe’ → 'c:\python39\Scripts\pygmentize.exe.deletem