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
So as I said when I was trying to run my program, I noticed below
ModuleNotFoundError: No module named 'sklearn' error on the output.
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.
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.