Sklearn or scikit learn is the most popular, free machine learning library for python. It includes widely used machine learning algorithms such as regression models, k nearest neighbors, k means clustering, support vector machine, dbscan, random forests, etc. Hence, there’s no surprise that data scientists or anyone with a passion for machine learning and Artificial Intelligence would want to install scikit learn in their systems. But a lot of developers have to face ModuleNotFoundError: No module named ‘sklearn’ once they try to execute their code. There can be a couple of reasons why they are seeing this error.
But first, What is a ModuleNotFoundError?
Simply A ModuleNotFoundError (In this particular case no module named ‘sklearn’ error) is raised at runtime when the python is unable/fails to import a module into your program. There are a few possible reasons behind this error.
- Python cannot locate the module in your import command
- You have misspelled your module name in your import command
- You simply haven’t installed that module
Common Cause of ModuleNotFoundError: No module named ‘sklearn’
In most cases of no module named ‘sklearn’, we can rule out the number two and number three causes that I have just mentioned in the above section.
Because most of the time we know that we have installed scikit learn beforehand. And we also know that we have typed the name correctly.
Then it must be that python cannot locate sklearn module, right? Yes, Of Course!
Reasons why Python cannot locate the sklearn/scikit learn module
The reasons can be very complicated because every case is different. So one’s cause for no module named ‘sklearn’ error might not be the same as someone else’s. There are many possible scenarios why python cannot locate sklearn module. Starting from different python virtual environments and different python versions to things like whether you are running your algorithm in jupyter notebooks or in an IDE like PyCharm, can all be a possible cause. So we have to investigate every single scenario and apply possible fixes. Otherwise, we aren’t addressing no module named ‘sklearn’ in everyone’s favor!
Are you a complete beginner to Scikit-Learn? Or do you want more hands-on tutorials? Then check out the following Scikit Learn Books for beginners.
Scikit Learn has been installed in a different virtual environment than your program being executed
The first thing you should do when you see this error is to check your python virtual environment. Is it the same environment that you installed scikit learn into? If not, then python can’t locate the sklearn module and hence raises the error.
I have installed all my machine learning libraries and packages into a default environment called “tensorflow“. Why named TensorFlow? It’s just because I am working heavily with TensorFlow projects currently. The name of the environment can be anything that you wish. It’s just a method for isolating and organizing your different python projects so everything is clean and separated. You don’t have to worry about version incompatibilities, etc. if you have different environments.
But now, let’s import sklearn into a different python environment in PyCharm and check whether python can locate it or not.
Yes, you guessed right! Python cannot locate sklearn module! We get the ModuleNotFoundError: No module named ‘sklearn’ error.
Let me edit my project environment and set it to tensorflow. And try again. Sown below.
Running the code again, with the correct environment (tensorflow) where sklearn has been installed.
Now as you can see the problem has been solved. Hence, we can continue with our machine learning code, there are numerous things to be done using scikit learn after all! 🙂
ModuleNotFoundError: No module named ‘sklearn’ error in Jupyter Notebooks
Not everyone’s running their ML programs in a real IDE. Often developers use jupyter as an easy python development environment that actually lives on web. When trying to run sklearn within jupyter notebooks people seem to face no module named sklearn error often.
Let’s now look for the possible causes for this no module named ‘sklearn’ errors raised in jupyter notebooks.
Again, we know that we have installed scikit learn and we have spelled the thing correctly. So the obvious cause behind the error is that python cannot find the sklearn module to import.
Best practice of installing Jupyter notebooks with scikit learn in your computer to avoid ModuleNotFoundError: No module named ‘sklearn’
The best way to install jupyter and to work with essential python packages and modules like sklearn by default is by installing the Anaconda distribution. I highly recommend installing Anaconda. Every machine learning enthusiast should have this amazing distribution for python installed. 🙂
Do you already have Anaconda installed? Then what version it is? Read this guide to find that out. Check Anaconda Version in Windows?
If you have installed the latest Anaconda version, you must already have jupyter notebooks and scikit learn pre-installed. Then you should be able to run jupyter notebook and import sklearn with no issue.
Simply go to your CMD and type and hit enter the following command.
This will automatically open up jupyter notebook for you.
Let’s start a new notebook and checking whether we can import sklearn with no errors
Success! There is no ModuleNotFoundError: No module named ‘sklearn’. I have the latest scikit- library which is 0.24.1.
No module named ‘sklearn’ error when jupyter notebooks and skleran installed separately
If you have installed jupyter notebooks and scikit-learn separately the chances are that jupyter notebooks cannot locate sklearn module to import.
For an example, you might have installed jupyter notebooks using another package manager like snap. Then Jupyter notebook will look for sklearn module inside the snap manager, which will raise an error ModuleNotFoundError: No module named ‘sklearn’.
Both Conda and pip install scikit-learn under the following path when using virtual environments.
But Jupyter notebook searches for the installed packages under,
Hence, it can’t locate the installed sklearn module. Then you get the infamous ModuleNotFoundError: No module named ‘sklearn’, yet again!
You can check the path of sklearn by running the following code in jupyter notebook.
To avoid path mismatch, we can install scikit learn always under,
Do you have several python versions installed in your computer?
When you have several python versions installed you are asking for trouble. Each python version must have its own path for \Lib\site-packages\ and its own set of packages and modules installed. So when your python machine learning program is looking for the sklearn module it literally doesn’t have any idea which python version’s path to look for. Hence raises ModuleNotFoundError: No module named ‘sklearn’ error.
As a solution, you can have one python version installed on your computer. If you do need several versions to be installed then you must install sklearn module into where jupyter notebook is pointing when it imports external modules.
ModuleNotFoundError: No module named ‘sklearn’ isn’t a complicated error. It’s a simple runtime error when python cannot import the specified module into your code. Most of the time this is because python cannot locate the path where sklearn module has been installed into. On other occasions, it’s we being so busy and misspelling the word “sklearn” in to something else. Lastly, in rare situations, it can be that we are trying to import the sklearn module when it’s hasn’t actually been installed! The bottom line is that this error isn’t something serious, you can always figure out what has gone wrong, and then fix it.
Want to become an ML and Data Science Expert? And get hired by world class companies? Enroll with Eduraka Today!