Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError: No module named sklearn

When I run:

from sklearn import datasets

I get the error:

ModuleNotFoundError: No module named 'sklearn'

How can I solve this?

like image 303
Imdadul Haque Avatar asked Jan 23 '26 12:01

Imdadul Haque


2 Answers

You may also try to install: scikit-learn

pip install scikit-learn

or via Conda:

conda install scikit-learn
like image 58
POlczak Avatar answered Jan 25 '26 19:01

POlczak


This confused me after reading several posts about this.

To make sure I was installing to the right Python, I did this:

python -m pip install sklearn

It said

Requirement already satisfied: sklearn in /home/.../lib/python3.10/site-packages (0.0.post5)

Then I typed python to get the prompt and then import sklearn. It said ModuleNotFoundError: No module named 'sklearn'. But I just installed it, right? Wrong!

I ran python -m pip show sklearn and it said

Name: sklearn
Version: 0.0.post5
Summary: deprecated sklearn package, use scikit-learn instead

This is saying sklearn isn't the package to install to get the module sklearn. Instead I should install scikit-learn to get the module sklearn.

So I ran python -m pip uninstall sklearn and then python -m pip install scikit-learn. Now when I open python and type import sklearn it imports scikit-learn.

I feel this is unnecessarily confusing (that sklearn isn't installed as sklearn) so I'm posting here in hopes that it helps someone else.

like image 38
Peter Rowlett Avatar answered Jan 25 '26 19:01

Peter Rowlett