0

Introduction

I need to use the 'KMeans' function from 'sklearn.cluster'. I am on PyCharm using Python 3.7 with a virtualvenv environment. I can add the necessary packages in order to use 'sklearn' using pip (I meen 'numpy', 'scipy', 'scikit-learn'...). Unfortunately there is an error when I run a python file in which I import 'sklearn'. I don't know why there is an error and how can I fixed it...


My problem is that the import is not working. When I run from sklearn.cluster import KMeans, it returns "OSError: [WinError 126]" :

Traceback (most recent call last):
  File "C:/Users/username/PycharmProjects/Test2/main.py", line 3, in <module>
    import sklearn
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\sklearn\__init__.py", line 80, in <module>
    from .base import clone
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\sklearn\base.py", line 21, in <module>
    from .utils import _IS_32BIT
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\sklearn\utils\__init__.py", line 20, in <module>
    from scipy.sparse import issparse
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\scipy\__init__.py", line 136, in <module>
    from . import _distributor_init
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\scipy\_distributor_init.py", line 61, in <module>
    WinDLL(os.path.abspath(filename))
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\ctypes\__init__.py", line 364, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

I think that there is a problem with the packages versions. If you want to see the packages versions (pip freeze result) :

numpy==1.19.3
scikit-learn==0.23.2
scipy==1.5.4
sklearn==0.0
threadpoolctl==2.1.0

Edit

I have found that there is a problem with scipy version. When I run 'import scipy' there is the same type of error (OSError: [WinError 126] The specified module could not be found). A solution is to downgrade scipy 1.5.4 to scipy 1.4.1 (ref Error when loading scipy: OSError: [WinError 126] The specified module could not be found). The [WinError 126] error disappears.

Unfortunately there is another problem coming. Now when I run from sklearn.cluster import KMeans, there is this error :

Traceback (most recent call last):
  File "C:/Users/username/PycharmProjects/Test2/main.py", line 1, in <module>
    from sklearn.cluster import KMeans
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\sklearn\__init__.py", line 80, in <module>
    from .base import clone
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\sklearn\base.py", line 21, in <module>
    from .utils import _IS_32BIT
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\sklearn\utils\__init__.py", line 23, in <module>
    from .class_weight import compute_class_weight, compute_sample_weight
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\sklearn\utils\class_weight.py", line 7, in <module>
    from .validation import _deprecate_positional_args
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\sklearn\utils\validation.py", line 25, in <module>
    from .fixes import _object_dtype_isnan, parse_version
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\sklearn\utils\fixes.py", line 18, in <module>
    import scipy.stats
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\scipy\stats\__init__.py", line 384, in <module>
    from .stats import *
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\scipy\stats\stats.py", line 179, in <module>
    from scipy.spatial.distance import cdist
  File "C:\Users\username\PycharmProjects\Test2\venv\lib\site-packages\scipy\spatial\__init__.py", line 99, in <module>
    from .qhull import *
ImportError: DLL load failed: The specified module could not be found.

It seems to be a more common error so I am checking.

  • Welcome to SO! This question is not clear. Are you trying to fix a broken package? Kindly consider rewording your post to be more specific. Also you may like to read [how to ask](https://stackoverflow.com/help/how-to-ask) and [on-topic questions at SO](https://stackoverflow.com/help/on-topic). – vyi Dec 10 '20 at 11:25
  • Thanks for your comment ! I have solved my problem and I hope it could help others. I will edit the question to make it more understandable. – STF_Maddox Dec 10 '20 at 12:05

2 Answers2

1

I encountered a similar problem and my solution was setting up a new virtual environment and using a python 3.9 interpreter instead of 3.7.

This way ImportError: DLL load failed: The specified module could not be found. was solved, too.

I hope you already solved this issue but maybe someone else will face this problem in the future

0

I find a solution : use conda python interpretor. There is maybe a problem with the virtualvenv environment of PyCharm.

Check this well known post if you want more details : In pycharm ImportError: DLL load failed: The specified module could not be found. while importing facerecognition