“sklearn找不到模块”问题解决

安装sklearn时遇到的一个小问题

出现的问题及其原因

安装好sklearn后进行测试时,出现了这样的错误:

Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\__init__.py", line 82, in <module>
    from .base import clone
  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py", line 20, in <module>
    from .utils import _IS_32BIT
  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\__init__.py", line 27, in <module>
    from .fixes import np_version
  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\fixes.py", line 18, in <module>
    import scipy.stats
  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\stats\__init__.py", line 345, in <module>
    from .stats import *
  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\stats\stats.py", line 169, in <module>
    import scipy.special as special
  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\special\__init__.py", line 640, in <module>
    from ._ufuncs import *
ImportError: DLL load failed: 找不到指定的模块。

这一问题出现的原因主要是sklearn的各个依赖包版本不兼容,可能有的是从官网上下载的whl文件,有的是在民间一些镜像网站上下载的文件,这就导致了版本之间的不兼容。

解决办法

当版本不兼容时,最好的办法就是集体换上官方的最新版本。

分析我们的报错信息,我们可以发现,我这里应该是scipy包的版本不兼容。(这里默认你的sklearn安装的是官方的,如果不是建议重新装一下)

重装就很简单了,我们用pip安装,不添加任何奇怪的指令直接安装,就默认安装的是最新版本了:

pip uninstall scipy
pip install scipy

我们在卸载scipy的时候,会出现一些红字报错,我们不用管这些信息,他不影响我们卸载依赖包:

ERROR: Exception:
Traceback (most recent call last):
  File "c:\programdata\anaconda3\lib\site-packages\pip-20.1-py3.7.egg\pip\_internal\cli\base_command.py", line 188, in _main
    status = self.run(options, args)
  File "c:\programdata\anaconda3\lib\site-packages\pip-20.1-py3.7.egg\pip\_internal\commands\uninstall.py", line 89, in run
    uninstall_pathset.commit()
  File "c:\programdata\anaconda3\lib\site-packages\pip-20.1-py3.7.egg\pip\_internal\req\req_uninstall.py", line 450, in commit
    self._moved_paths.commit()
  File "c:\programdata\anaconda3\lib\site-packages\pip-20.1-py3.7.egg\pip\_internal\req\req_uninstall.py", line 290, in commit
    save_dir.cleanup()
  File "c:\programdata\anaconda3\lib\site-packages\pip-20.1-py3.7.egg\pip\_internal\utils\temp_dir.py", line 196, in cleanup
    rmtree(self._path)
  File "c:\programdata\anaconda3\lib\site-packages\pip-20.1-py3.7.egg\pip\_vendor\retrying.py", line 49, in wrapped_f
    return Retrying(*dargs, **dkw).call(f, *args, **kw)
  File "c:\programdata\anaconda3\lib\site-packages\pip-20.1-py3.7.egg\pip\_vendor\retrying.py", line 212, in call
    raise attempt.get()
  File "c:\programdata\anaconda3\lib\site-packages\pip-20.1-py3.7.egg\pip\_vendor\retrying.py", line 247, in get
    six.reraise(self.value[0], self.value[1], self.value[2])
  File "c:\programdata\anaconda3\lib\site-packages\pip-20.1-py3.7.egg\pip\_vendor\six.py", line 703, in reraise
    raise value
  File "c:\programdata\anaconda3\lib\site-packages\pip-20.1-py3.7.egg\pip\_vendor\retrying.py", line 200, in call
    attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
  File "c:\programdata\anaconda3\lib\site-packages\pip-20.1-py3.7.egg\pip\_internal\utils\misc.py", line 136, in rmtree
    onerror=rmtree_errorhandler)
  File "C:\ProgramData\Anaconda3\lib\shutil.py", line 507, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\ProgramData\Anaconda3\lib\shutil.py", line 386, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\ProgramData\Anaconda3\lib\shutil.py", line 386, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\ProgramData\Anaconda3\lib\shutil.py", line 391, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\ProgramData\Anaconda3\lib\shutil.py", line 389, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] 拒绝访问。: 'c:\\programdata\\anaconda3\\lib\\site-packages\\~cipy\\sparse\\csgraph\\_min_spanning_tree.cp37-win_amd64.pyd'

只要出现这样的语句,就代表我们卸载成功了:

Successfully uninstalled scipy-1.1.0

安装后出现如下的信息,表示安装成功了:

Collecting scipy
  Downloading scipy-1.4.1-cp37-cp37m-win_amd64.whl (30.9 MB)
     |████████████████████████████████| 30.9 MB 473 kB/s
Requirement already satisfied: numpy>=1.13.3 in c:\programdata\anaconda3\lib\site-packages (from scipy) (1.18.3)
Installing collected packages: scipy
Successfully installed scipy-1.4.1

再次测试sklearn,发现成功载入,我们的sklearn就可以正常使用了

Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
>>>

最后的忠告

遇事不决用官方(虽然一些境外网站的下载速度可能很感人),否则你永远不知道哪个民间包不兼容就会毁了你的新环境配置,555。

参考资料

1.python import sklearn出错 "ImportError: DLL load failed: 找不到指定的模块:https://blog.csdn.net/HANNING563128766/article/details/80023613

(0)

相关推荐