People new to Python usually have trouble installing the scikit-learn
package, which is the de-facto machine learning library. A very common error when it comes to importing the package in their source code is the ModuleNotFoundError
.
ModuleNotFoundError: No module named 'sklearn'
This error indicates that the scikit-learn
(sklearn
) package was not installed, or even if it was installed, it can’t be resolved.
5 Solutions for the ModuleNotFoundError: No Module Named ‘sklearn’
- Install packages the right way with
pip
. - Upgrade
sklearn
to the latest version. - Install
sklearn
to the right virtual environment. - Install
sklearn
to the correct Anaconda environment. - Install Jupyter Notebook to the same environment as
sklearn
.
In this tutorial, I’ll go through a few basic concepts when it comes to installing packages on Python that could eventually help you get rid of this error and start working on your machine learning projects. More specifically, we will discuss:
- How to install packages through
pip
. - How to upgrade
sklearn
to the latest version. - How to properly use virtual environments and manage package versions.
- What to do if you are facing this issue with Anaconda.
- What to do if you are getting this error in a Jupyter Notebook.
5 Causes for ModuleNotFoundError: No Module Named ‘Sklearn’ and Solutions
There are a number of places that could be causing the ModuleNotFoundError
. Let’s take a look at each one:
1. Install Sklearn With Pip the Right Way
You may have multiple Python versions installed on your local machine. Every time you install a package, this installation is associated with just a single version. There’s a chance that you’ve installed scikit-learn
for one Python version, but you’re executing your source code using a different version. This may be the reason why scikit-learn
can’t be found.
Therefore, make sure you use the correct command to install sklearn
through pip
. Typically, users attempt to install packages using the command:
$ pip install package_name
Or:
$ pip3 install package_name
Both of the above commands are going to install the specified package that Python is associated with. For instance, you can find out by running:
$ pip --version
pip 19.0.3 from /usr/lib/python3.7/site-packages/pip (python 3.7)
Solution
To resolve this, make sure you use the following notation when installing Python packages through pip
:
$ python3 -m pip install scikit-learn
This is going to ensure that the package to be installed, will be available to the Python version you will be using to run your source code. You can find where that specific Python executable is located on your local machine by executing:
$ which python3
2. Upgrade Sklearn to the Latest Version
Additionally, it may be helpful to ensure that you are using the latest version of scikit-learn
and not an old one.
Solution
To update to the most recent version available you can run the following command:
$ python3 -m pip install -U scikit-learn
3. install Sklearn in the Right Virtual Environment
Python’s venv
module allows the creation of virtual environments. Every virtual environment is completely isolated and has its own Python binary. Additionally, it may also have its own set of installed packages within its own site directory.
This means that if a package is installed within a specific virtual environment, it won’t be visible to the system-wide installed packages or any other virtual environment.
If you aren’t currently using a virtual environment, I would advise you to start doing so because it will greatly help you manage package dependencies easier and more efficiently.
Solution
Going to our specific use-case, if you wish to work on a project that requires scikit-learn, then you essentially have to follow three steps:
First, create a virtual environment for your project and place it into your desired location. Let’s create one using the name my_project_venv
.
$ python -m venv /path/to/your/venv/called/my_project_venv
Now that the virtual environment has been created, you should activate it. You can do so using the following command:
$ source /path/to/your/venv/called/my_project_venv/bin/activate
If the virtual environment has been activated successfully, you should be able to see the venv
name as a prefix in your command line, for example (my_project_venv
).
Now, you can finally install sklearn
, and any other dependency you need to build your Python application, using the commands we discussed earlier.
(my_project_venv) $ python3 -m pip install scikit-learn
And eventually execute your script:
(my_project_venv) $ python3 my_script_using_sklearn.py
4. Install SKlearn to the Correct Anaconda Environment
If you are currently working with conda, you may have to be careful with which environment you are actually working in.
If you want to install scikit-learn
at the root, which isn’t recommended, then you could do so using:
$ conda install scikit-learn
Solution
Alternatively, if you want to install the scikit-learn
package to a specific Anaconda environment, then you can use the -n
flag to specify the environment name. For example, the following command will install scikit-learn
to the conda environment called my_environment
:
conda install -n my_environment scikit-learn
If none of the above approaches work, then you can still install scikit-learn
through pip
, even when working within a conda environment. In an Anaconda prompt, simply run:
$ pip install scikit-learn
5. Install Jupyter Notebook to the Same Environment as Sklearn
Finally, if you are getting the ModuleNotFoundError
in a Jupyter Notebook, then you need to ensure that both Jupyter and scikit-learn
are installed within the same environment.
Solution
The first step is to check the path to which the Jupyter Notebook is installed on your local machine. For example:
$ which jupyter
$ which jupyter-notebook
As I mentioned already, it’s much better, and will save you time and energy, if you work in isolated environments.
$ conda install -c anaconda ipython
Or
conda install -c anaconda jupyter
If you are working in a specific conda environment, then install both the Jupyter Notebook and the scikit-learn
package within the same environment:
$ conda install -n my_environment jupyter
$ conda install -n my_environment scikit-learn
If you are working in a Python virtual environment (venv
), then:
$ python3 -m pip install jupyter
$ python3 -m pip install scikit-learn
And finally, open your Jupyter Notebook from the activated environment and import scikit-learn. You should now be good to go!
Understanding ModuleNotFoundError: No Module Named ‘Sklearn’
In the article, we discussed the main causes of ModuleNotFoundError
when it comes to importing sklearn
in your Python source code. Additionally, we explored a few different topics that may be relevant to this issue and can eventually help you resolve the import error.
More specifically, we discussed how to resolve the issue using proper pip installation commands, how to manage your projects in isolated virtual environments and how to overcome the ModuleNotFoundError
when working with Anaconda and Jupyter Notebooks.