In this article, I will show you how to solve ModuleNotFoundError: No module named 'sklearn'
error in Python3. It is not very uncommon to get this error in a freshly built system which has been given to Python programmers or developers to run their programs. Something similar happened to me as well, for the first time I tried to use scikit-learn module in my program but when i tried to run it, I ended up with ModuleNotFoundError: No module named 'sklearn'
error. I am sure lot of folks out there might be facing the same issue so before solving this error I thought to write an article about this so that it will help you guys also.
Solved: ModuleNotFoundError: No module named 'sklearn' in Python3
Also Read: Python: Introduction to Pipenv and Poetry with Best Examples
So as I said when I was trying to run my program, I noticed below ModuleNotFoundError: No module named 'sklearn'
error on the output.
NOTE:
cyberithub@ubuntu:~$ python3 example.py Traceback (most recent call last): File "example.py", line 1, in <module> import sklearn as sk ModuleNotFoundError: No module named 'sklearn'
While the above error could occur due to multiple reasons but most of the time it occurs due to missing sklearn
module in your System. So to solve this error you just need to install sklearn module in your System. If you have pip3
utility available in your system then you can easily install the module by using pip3 install -U scikit-learn
command as shown below.
cyberithub@ubuntu:~$ pip3 install -U scikit-learn
Collecting scikit-learn
Downloading scikit_learn-1.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.2 MB)
|████████████████████████████████| 31.2 MB 111 kB/s
Collecting joblib>=1.0.0
Downloading joblib-1.1.0-py2.py3-none-any.whl (306 kB)
|████████████████████████████████| 306 kB 4.3 MB/s
Requirement already satisfied, skipping upgrade: numpy>=1.17.3 in /usr/lib/python3/dist-packages (from scikit-learn) (1.17.4)
Collecting threadpoolctl>=2.0.0
Downloading threadpoolctl-3.1.0-py3-none-any.whl (14 kB)
Collecting scipy>=1.3.2
Downloading scipy-1.9.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (43.4 MB)
|████████████████████████████████| 43.4 MB 94 kB/s
ERROR: scipy 1.9.1 has requirement numpy<1.25.0,>=1.18.5, but you'll have numpy 1.17.4 which is incompatible.
Installing collected packages: joblib, threadpoolctl, scipy, scikit-learn
Successfully installed joblib-1.1.0 scikit-learn-1.1.2 scipy-1.9.1 threadpoolctl-3.1.0
Alternatively, on a Debian/Ubuntu based system, you can install the module by using sudo apt install python3-sklearn
command as shown below.
cyberithub@ubuntu:~$ sudo apt install python3-sklearn [sudo] password for cyberithub: Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: blt fonts-lyx javascript-common libjs-jquery libjs-jquery-ui liblbfgsb0 libtcl8.6 libtk8.6 python-matplotlib-data python3-atomicwrites python3-cycler python3-decorator python3-importlib-metadata python3-joblib python3-kiwisolver python3-matplotlib python3-more-itertools python3-nose python3-packaging python3-pluggy python3-py python3-pyparsing python3-pytest python3-scipy python3-sklearn-lib python3-tk python3-wcwidth python3-zipp tk8.6-blt2.5 ttf-bitstream-vera Suggested packages: blt-demo apache2 | lighttpd | httpd libjs-jquery-ui-docs tcl8.6 tk8.6 python-cycler-doc dvipng ffmpeg inkscape ipython3 python-matplotlib-doc python3-gobject python3-pyqt5 python3-tornado texlive-extra-utils texlive-latex-extra ttf-staypuft python-nose-doc subversion python-pyparsing-doc python-scipy-doc python3-dap python-sklearn-doc tix python3-tk-dbg The following NEW packages will be installed: blt fonts-lyx javascript-common libjs-jquery libjs-jquery-ui liblbfgsb0 libtcl8.6 libtk8.6 python-matplotlib-data python3-atomicwrites python3-cycler python3-decorator python3-importlib-metadata python3-joblib python3-kiwisolver python3-matplotlib python3-more-itertools python3-nose python3-packaging python3-pluggy python3-py python3-pyparsing python3-pytest python3-scipy python3-sklearn python3-sklearn-lib python3-tk python3-wcwidth python3-zipp tk8.6-blt2.5 ttf-bitstream-vera 0 upgraded, 31 newly installed, 0 to remove and 51 not upgraded. Need to get 27.7 MB of archives. After this operation, 110 MB of additional disk space will be used. Do you want to continue? [Y/n] Y .........................................
After installing the scikit-learn
module, I again tried to run the program and guess what. This time it ran successfully. This confirms that my issue is resolved now. But there could be a chance that you might be still facing the issue and the same solution does not work for you. So in that case you need to check if the python libraries path are visible to System or not. If you have installed the libraries in different path then you need to specify the location in PATH
environment variable to make it visible to the System.
Hopefully, above given solution would be helpful to you as well. Please let me know your feedback on the comment box.