How to Solve scikit-learn Error when importing scikit-learn modules

scikit-learn Error when importing scikit-learn modules is a Python error which occurs when you have a faulty Numpy installation.

In this article I am going to solve the error while I try explaining why the error is popping up in the first place, I will also introduce some solutions which worked for other developers and we will see if those solutions can solve the error in your unique situation.

Explaining the Error : scikit-learn Error when importing scikit-learn modules

The problem happens when we try to import scikit-learn modules or any modules while having a faulty Numpy installation.

If you use an import like in the example bellow, the error message should look like the error in the same example.

                                                                       #
import sklearn
db = sklearn.cluster.DBSCAN()
#------------------------error message-----------------------#
AttributeError: 'module' object has no attribute 'cluster'
                                                                       #

If we use another format, we get the following error like in the example bellow.

                                                                       #
from sklearn import cluster
#------------------------error message-----------------------#
Traceback (most recent call last):
  File "example.py", line 99, in <module>
    from sklearn import cluster
.....
    from . import _iterative
ImportError: DLL load failed: The specified module could not be found.
                                                                       #

Just make sure your error message is similar to the ones we have above.

Bellow are the solutions which have worked for me and will help you to successfully solve this issue.

Solution 1 : beware from where you download

First, we should understand that any library or package should be downloaded from the official source.

Getting The unofficial scipy and numpy installers from the link bellow does not always lead to a non faulty installation.

                                                                       #
http://www.lfd.uci.edu/~gohlke/pythonlibs/
                                                                       #

However, downloading from the official installer in the link bellow is the better idea.

                                                                       #
http://www.scipy.org/install.html
                                                                       #

If you have numpy installed from an unofficial source, remove the installation and re-install again.

This solution should be enough to solve the error but if this is not the trick try the solution bellow.

Solution 2 : use MKL and Numpy – 64bit versions

The second solution is to use MKL and numpy 64 bit versions.

Just make sure to get the latest 64bit version if you can.

It is possible that you are getting the error because you are only using Numpy.

You can run the following command in order to reinstall Numpy with MKL.

                                                                       #
pip install --upgrade --force-reinstall "numpy‑1.16.3+mkl‑cp37‑cp37m‑win32.whl"
                                                                       #

The error should be gone. If that is not the case, make sure to try the solution bellow.

Solution 3 : uninstall/ scipy and then install again

Another solution is to make sure to uninstall scipy and then install it again.

First uninstall scipy using the following command.

                                                                       #
pip uninstall scipy
                                                                       #

And then install it using the command bellow.

                                                                       #
pip install scipy
                                                                       #

I hope this solution has solved your issue. If it did not, try using the last solution in our blog post.

If this article has been useful for your particular case, consider donating to our Kofi account, there is a red button at the top of this page.

Summing-up

This is it, it is the end of our article, I hope I helped you solve this Python error, a simple upgrade will usually solve this issue. Do not give up, keep coding and learning, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/