Solving scikit-learn ModuleNotFoundError: No module named ‘sklearn’ ( sometimes happens with anaconda and conda )

Scikit-learn ModuleNotFoundError: No module named ‘sklearn’ ( sometimes happens with anaconda and conda ) is an error which occurs a lot when you try to import sklearn.

In today’s blog post I am going to present an annoying and confusing python error and explain why this error is taking place and how to fix it, with a set of possible fixes.

Exploring the scikit-learn ModuleNotFoundError: No module named ‘sklearn’ ( sometimes happens with anaconda and conda )

This is an error which occurs a lot when you try to import sklearn.

Beware of mixing between different errors. Please make sure you are dealing with the same error.

                                                                       #
ModuleNotFoundError: No module named 'sklearn'
                                                                       #

Bellow, I will present multiple method that have worked for me and others devs.

Solution 1 : Install matplotlib, scipy and scikit-learn using pip

If you try to import sklearn while using anaconda chances are you are going to encounter this error.

The solution is to install matplotlib, scipy and scikit-learn using pip, just like in the command bellow.

                                                                       #
pip install -U scikit-learn scipy matplotlib
                                                                       #

If this does not work, try starting the command with sudo, just like in the command bellow.

                                                                       #
sudo pip install -U scikit-learn scipy matplotlib
                                                                       #

If you have python3 use pip3 instead of pip, like this.

                                                                       #
sudo pip3 install -U scikit-learn scipy matplotlib
                                                                       #

Or try this command

                                                                       #
pip3 install -U scikit-learn scipy matplotlib
                                                                       #

The method above should work, try the second method instead, if this one does not work.

Solution 2 : Use sudo and install python3-sklearn

The second solution is composed of two options that you can try separately or at the same time.

First, you can try the command bellow

                                                                       #
sudo apt install python3-sklearn 
                                                                       #

The second option is to try this

                                                                       #
import sys
!{sys.executable} -m pip install sklearn
                                                                       #

I hope the methods above fixed your problem. Thank you for reaching the end of this blog post.

Summing-up : 

That is it guys, this is the end of this article/guide, I hope you found it useful in solving your particular problem, you can support our work on our Kofi account.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/