导入 scikit-learn 时出现问题:模块“scipy”没有属性“_lib”

问题描述 投票:0回答:3

我是 Python 新手,正在 Windows 10 上使用 Anaconda 来学习如何实现机器学习。在 Spyder 上运行此代码:

import sklearn as skl

最初给我的是这个:

Traceback (most recent call last):

  File "<ipython-input-1-7135d3f24347>", line 1, in <module>
    runfile('C:/Users/julia/.spyder-py3/temp.py', wdir='C:/Users/julia/.spyder-py3')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/julia/.spyder-py3/temp.py", line 3, in <module>
    from sklearn.family import Model

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\__init__.py", line 76, in <module>
    from .base import clone

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py", line 16, in <module>
    from .utils import _IS_32BIT

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\__init__.py", line 20, in <module>
    from .validation import (as_float_array,

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 21, in <module>
    from .fixes import _object_dtype_isnan

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\fixes.py", line 289, in <module>
    from scipy.sparse.linalg import lsqr as sparse_lsqr

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\linalg\__init__.py", line 114, in <module>
    from .isolve import *

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6, in <module>
    from .iterative import *

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 10, in <module>
    from . import _iterative

ImportError: DLL load failed: The specified module could not be found.

然后我进入命令行并执行了

pip uninstall scipy
pip install scipy

pip uninstall scikit-learn
pip install scikit-learn

这样做时没有出现任何错误,根据命令行安装了 scipy 1.3.1(以及 numpy 1.17.0)和 scikit-learn 0.21.3。

但是,现在当我尝试导入 sklearn 时,出现了不同的错误:

 File "<ipython-input-2-7135d3f24347>", line 1, in <module>
    runfile('C:/Users/julia/.spyder-py3/temp.py', wdir='C:/Users/julia/.spyder-py3')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/julia/.spyder-py3/temp.py", line 3, in <module>
    from sklearn.family import Model

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\__init__.py", line 76, in <module>
    from .base import clone

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py", line 16, in <module>
    from .utils import _IS_32BIT

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\__init__.py", line 20, in <module>
    from .validation import (as_float_array,

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 21, in <module>
    from .fixes import _object_dtype_isnan

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\fixes.py", line 289, in <module>
    from scipy.sparse.linalg import lsqr as sparse_lsqr

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\linalg\__init__.py", line 113, in <module>
    from .isolve import *

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6, in <module>
    from .iterative import *

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 136, in <module>
    def bicg(A, b, x0=None, tol=1e-5, maxiter=None, M=None, callback=None, atol=None):

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\_lib\_threadsafety.py", line 59, in decorator
    return lock.decorate(func)

  File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\_lib\_threadsafety.py", line 47, in decorate
    return scipy._lib.decorator.decorate(func, caller)

AttributeError: module 'scipy' has no attribute '_lib'

有什么建议吗?我已经卸载并重新安装了 Anaconda,但仍然遇到同样的问题。

编辑:当我这样做时

conda list --show-channel-urls

我明白了

# packages in environment at C:\ProgramData\Anaconda3:
#
# Name                    Version                   Build  Channel
_ipyw_jlab_nb_ext_conf    0.1.0                    py37_0    defaults
alabaster                 0.7.12                   py37_0    defaults
anaconda-client           1.7.2                    py37_0    defaults
anaconda-navigator        1.9.7                    py37_0    defaults
asn1crypto                0.24.0                   py37_0    defaults
astroid                   2.2.5                    py37_0    defaults
attrs                     19.1.0                   py37_1    defaults
babel                     2.7.0                      py_0    defaults
backcall                  0.1.0                    py37_0    defaults
backports                 1.0                        py_2    defaults
backports.functools_lru_cache 1.5                        py_2    defaults
backports.tempfile        1.0                        py_1    defaults
backports.weakref         1.0.post1                  py_1    defaults
beautifulsoup4            4.7.1                    py37_1    defaults
blas                      1.0                         mkl    defaults
bleach                    3.1.0                    py37_0    defaults
bzip2                     1.0.8                he774522_0    defaults
ca-certificates           2019.5.15                     1    defaults
certifi                   2019.6.16                py37_1    defaults
cffi                      1.12.3           py37h7a1dbc1_0    defaults
chardet                   3.0.4                 py37_1003    defaults
click                     7.0                      py37_0    defaults
cloudpickle               1.2.1                      py_0    defaults
clyent                    1.2.2                    py37_1    defaults
colorama                  0.4.1                    py37_0    defaults
conda                     4.7.11                   py37_0    defaults
conda-build               3.18.8                   py37_0    defaults
conda-env                 2.6.0                         1    defaults
conda-package-handling    1.3.11                   py37_0    defaults
conda-verify              3.4.2                      py_1    defaults
console_shortcut          0.1.1                         3    defaults
cryptography              2.7              py37h7a1dbc1_0    defaults
decorator                 4.4.0                    py37_1    defaults
defusedxml                0.6.0                      py_0    defaults
docutils                  0.15.1                   py37_0    defaults
entrypoints               0.3                      py37_0    defaults
filelock                  3.0.12                     py_0    defaults
freetype                  2.9.1                ha9979f8_1    defaults
future                    0.17.1                   py37_0    defaults
glob2                     0.7                        py_0    defaults
icc_rt                    2019.0.0             h0cc432a_1    defaults
icu                       58.2                 ha66f8fd_1    defaults
idna                      2.8                      py37_0    defaults
imagesize                 1.1.0                    py37_0    defaults
intel-openmp              2019.4                      245    defaults
ipykernel                 5.1.1            py37h39e3cac_0    defaults
ipython                   7.7.0            py37h39e3cac_0    defaults
ipython_genutils          0.2.0                    py37_0    defaults
ipywidgets                7.5.1                      py_0    defaults
isort                     4.3.21                   py37_0    defaults
jedi                      0.13.3                   py37_0    defaults
jinja2                    2.10.1                   py37_0    defaults
joblib                    0.13.2                   py37_0    defaults
jpeg                      9b                   hb83a4c4_2    defaults
json5                     0.8.5                      py_0    defaults
jsonschema                3.0.1                    py37_0    defaults
jupyter_client            5.3.1                      py_0    defaults
jupyter_core              4.5.0                      py_0    defaults
jupyterlab                1.0.2            py37hf63ae98_0    defaults
jupyterlab_server         1.0.0                      py_1    defaults
keyring                   18.0.0                   py37_0    defaults
lazy-object-proxy         1.4.1            py37he774522_0    defaults
libarchive                3.3.3                h0643e63_5    defaults
libiconv                  1.15                 h1df5818_7    defaults
liblief                   0.9.0                ha925a31_2    defaults
libpng                    1.6.37               h2a8f88b_0    defaults
libsodium                 1.0.16               h9d3ae62_0    defaults
libtiff                   4.0.10               hb898794_2    defaults
libxml2                   2.9.9                h464c3ec_0    defaults
lz4-c                     1.8.1.2              h2fa13f4_0    defaults
lzo                       2.10                 h6df0209_2    defaults
m2w64-gcc-libgfortran     5.3.0                         6    defaults
m2w64-gcc-libs            5.3.0                         7    defaults
m2w64-gcc-libs-core       5.3.0                         7    defaults
m2w64-gmp                 6.1.0                         2    defaults
m2w64-libwinpthread-git   5.0.0.4634.697f757               2    defaults
markupsafe                1.1.1            py37he774522_0    defaults
mccabe                    0.6.1                    py37_1    defaults
menuinst                  1.4.16           py37he774522_0    defaults
mistune                   0.8.4            py37he774522_0    defaults
mkl                       2019.4                      245    defaults
mkl-service               2.0.2            py37he774522_0    defaults
mkl_fft                   1.0.12           py37h14836fe_0    defaults
mkl_random                1.0.2            py37h343c172_0    defaults
msys2-conda-epoch         20160418                      1    defaults
navigator-updater         0.2.1                    py37_0    defaults
nbconvert                 5.5.0                      py_0    defaults
nbformat                  4.4.0                    py37_0    defaults
notebook                  6.0.0                    py37_0    defaults
numpy                     1.17.0                   pypi_0    pypi
numpy-base                1.16.4           py37hc3f5095_0    defaults
numpydoc                  0.9.1                      py_0    defaults
olefile                   0.46                     py37_0    defaults
openssl                   1.1.1c               he774522_1    defaults
packaging                 19.0                     py37_0    defaults
pandas                    0.25.0           py37ha925a31_0    defaults
pandoc                    2.2.3.2                       0    defaults
pandocfilters             1.4.2                    py37_1    defaults
parso                     0.5.0                      py_0    defaults
pickleshare               0.7.5                    py37_0    defaults
pillow                    6.1.0            py37hdc69c19_0    defaults
pip                       19.2.2                   pypi_0    pypi
pkginfo                   1.5.0.1                  py37_0    defaults
powershell_shortcut       0.0.1                         2    defaults
prometheus_client         0.7.1                      py_0    defaults
prompt_toolkit            2.0.9                    py37_0    defaults
psutil                    5.6.3            py37he774522_0    defaults
py-lief                   0.9.0            py37ha925a31_2    defaults
pycodestyle               2.5.0                    py37_0    defaults
pycosat                   0.6.3            py37hfa6e2cd_0    defaults
pycparser                 2.19                     py37_0    defaults
pyflakes                  2.1.1                    py37_0    defaults
pygments                  2.4.2                      py_0    defaults
pylint                    2.3.1                    py37_0    defaults
pyopenssl                 19.0.0                   py37_0    defaults
pyparsing                 2.4.0                      py_0    defaults
pyqt                      5.9.2            py37h6538335_2    defaults
pyrsistent                0.14.11          py37he774522_0    defaults
pysocks                   1.7.0                    py37_0    defaults
python                    3.7.3                h8c8aaf0_1    defaults
python-dateutil           2.8.0                    py37_0    defaults
python-libarchive-c       2.8                     py37_13    defaults
pytz                      2019.1                     py_0    defaults
pywin32                   223              py37hfa6e2cd_1    defaults
pywinpty                  0.5.5                 py37_1000    defaults
pyyaml                    5.1.1            py37he774522_0    defaults
pyzmq                     18.0.0           py37ha925a31_0    defaults
qt                        5.9.7            vc14h73c81de_0    defaults
qtawesome                 0.5.7                    py37_1    defaults
qtconsole                 4.5.2                      py_0    defaults
qtpy                      1.8.0                      py_0    defaults
requests                  2.22.0                   py37_0    defaults
rope                      0.14.0                     py_0    defaults
ruamel_yaml               0.15.46          py37hfa6e2cd_0    defaults
scikit-learn              0.21.3                   pypi_0    pypi
scipy                     1.3.0                    pypi_0    pypi
send2trash                1.5.0                    py37_0    defaults
setuptools                41.0.1                   py37_0    defaults
sip                       4.19.8           py37h6538335_0    defaults
six                       1.12.0                   py37_0    defaults
snowballstemmer           1.9.0                      py_0    defaults
soupsieve                 1.9.2                    py37_0    defaults
sphinx                    2.1.2                      py_0    defaults
sphinxcontrib-applehelp   1.0.1                      py_0    defaults
sphinxcontrib-devhelp     1.0.1                      py_0    defaults
sphinxcontrib-htmlhelp    1.0.2                      py_0    defaults
sphinxcontrib-jsmath      1.0.1                      py_0    defaults
sphinxcontrib-qthelp      1.0.2                      py_0    defaults
sphinxcontrib-serializinghtml 1.1.3                      py_0    defaults
spyder                    3.3.6                    py37_0    defaults
spyder-kernels            0.5.1                    py37_0    defaults
sqlite                    3.29.0               he774522_0    defaults
terminado                 0.8.2                    py37_0    defaults
testpath                  0.4.2                    py37_0    defaults
tk                        8.6.8                hfa6e2cd_0    defaults
tornado                   6.0.3            py37he774522_0    defaults
tqdm                      4.32.1                     py_0    defaults
traitlets                 4.3.2                    py37_0    defaults
urllib3                   1.24.2                   py37_0    defaults
vc                        14.1                 h0510ff6_4    defaults
vs2015_runtime            14.15.26706          h3a45250_4    defaults
wcwidth                   0.1.7                    py37_0    defaults
webencodings              0.5.1                    py37_1    defaults
wheel                     0.33.4                   py37_0    defaults
widgetsnbextension        3.5.0                    py37_0    defaults
win_inet_pton             1.1.0                    py37_0    defaults
wincertstore              0.2                      py37_0    defaults
winpty                    0.4.3                         4    defaults
wrapt                     1.11.2           py37he774522_0    defaults
xz                        5.2.4                h2fa13f4_4    defaults
yaml                      0.1.7                hc54c509_2    defaults
zeromq                    4.3.1                h33f27b4_3    defaults
zlib                      1.2.11               h62dcd97_3    defaults
zstd                      1.3.7                h508b16e_0    defaults

scipy 的版本与 pip 安装的版本不匹配。不确定它有多重要,但对我来说似乎很奇怪。

编辑2: 做

pip list
有回报

Package                       Version
----------------------------- ---------
-cipy                         1.3.0
alabaster                     0.7.12
anaconda-client               1.7.2
anaconda-navigator            1.9.7
asn1crypto                    0.24.0
astroid                       2.2.5
attrs                         19.1.0
Babel                         2.7.0
backcall                      0.1.0
backports.functools-lru-cache 1.5
backports.tempfile            1.0
backports.weakref             1.0.post1
beautifulsoup4                4.7.1
bleach                        3.1.0
certifi                       2019.6.16
cffi                          1.12.3
chardet                       3.0.4
Click                         7.0
cloudpickle                   1.2.1
clyent                        1.2.2
colorama                      0.4.1
conda                         4.7.11
conda-build                   3.18.8
conda-package-handling        1.3.11
conda-verify                  3.4.2
cryptography                  2.7
decorator                     4.4.0
defusedxml                    0.6.0
docutils                      0.15.1
entrypoints                   0.3
filelock                      3.0.12
future                        0.17.1
glob2                         0.7
idna                          2.8
imagesize                     1.1.0
ipykernel                     5.1.1
ipython                       7.7.0
ipython-genutils              0.2.0
ipywidgets                    7.5.1
isort                         4.3.21
jedi                          0.13.3
Jinja2                        2.10.1
joblib                        0.13.2
json5                         0.8.5
jsonschema                    3.0.1
jupyter-client                5.3.1
jupyter-core                  4.5.0
jupyterlab                    1.0.2
jupyterlab-server             1.0.0
keyring                       18.0.0
lazy-object-proxy             1.4.1
libarchive-c                  2.8
MarkupSafe                    1.1.1
mccabe                        0.6.1
menuinst                      1.4.16
mistune                       0.8.4
mkl-fft                       1.0.12
mkl-random                    1.0.2
mkl-service                   2.0.2
navigator-updater             0.2.1
nbconvert                     5.5.0
nbformat                      4.4.0
notebook                      6.0.0
numpy                         1.17.0
numpydoc                      0.9.1
olefile                       0.46
packaging                     19.0
pandas                        0.25.0
pandocfilters                 1.4.2
parso                         0.5.0
pickleshare                   0.7.5
Pillow                        6.1.0
pio                           0.0.3
pip                           19.2.2
pkginfo                       1.5.0.1
prometheus-client             0.7.1
prompt-toolkit                2.0.9
psutil                        5.6.3
pycodestyle                   2.5.0
pycosat                       0.6.3
pycparser                     2.19
pyflakes                      2.1.1
Pygments                      2.4.2
pylint                        2.3.1
pyOpenSSL                     19.0.0
pyparsing                     2.4.0
pyrsistent                    0.14.11
PySocks                       1.7.0
python-dateutil               2.8.0
pytz                          2019.1
pywin32                       223
pywinpty                      0.5.5
PyYAML                        5.1.1
pyzmq                         18.0.0
QtAwesome                     0.5.7
qtconsole                     4.5.2
QtPy                          1.8.0
requests                      2.22.0
rope                          0.14.0
ruamel-yaml                   0.15.46
scikit-learn                  0.21.3
scipy                         1.3.1
Send2Trash                    1.5.0
setuptools                    41.0.1
six                           1.12.0
snowballstemmer               1.9.0
soupsieve                     1.9.2
Sphinx                        2.1.2
sphinxcontrib-applehelp       1.0.1
sphinxcontrib-devhelp         1.0.1
sphinxcontrib-htmlhelp        1.0.2
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.2
sphinxcontrib-serializinghtml 1.1.3
spyder                        3.3.6
spyder-kernels                0.5.1
terminado                     0.8.2
testpath                      0.4.2
tornado                       6.0.3
tqdm                          4.32.1
traitlets                     4.3.2
urllib3                       1.24.2
wcwidth                       0.1.7
webencodings                  0.5.1
wheel                         0.33.4
widgetsnbextension            3.5.0
win-inet-pton                 1.1.0
wincertstore                  0.2
wrapt                         1.11.2

pip list
表示scipy是1.3.1版本,而
conda list
表示它是1.3.0版本。再次,不确定它有多相关,但看起来很奇怪

编辑3:在命令提示符中输入以下几行(由@Brennan建议)然后运行文件后,我收到此错误

pip uninstall scikit-learn
pip uninstall scipy
conda uninstall scikit-learn
conda uninstall scipy

conda update --all
conda install scipy
conda install scikit-learn

这是我在尝试导入 sklearn 时遇到的新错误:

Traceback (most recent call last):

  File "<ipython-input-15-7135d3f24347>", line 1, in <module>
    runfile('C:/Users/julia/.spyder-py3/temp.py', wdir='C:/Users/julia/.spyder-py3')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/julia/.spyder-py3/temp.py", line 2, in <module>
    import sklearn as skl

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\__init__.py", line 76, in <module>
    from .base import clone

  File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py", line 13, in <module>
    import numpy as np

  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
    from . import _distributor_init

  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
    from . import _mklinit

ImportError: DLL load failed: The specified module could not be found.

造成此问题的可能原因可能是我在遇到此处描述的错误后从 Anaconda/Library/bin 中删除了 mkl_rt.dll 文件:https://github.com/ContinuumIO/anaconda-issues/issues/10182

这让我陷入了困境,因为重新安装 Anaconda 来修复此问题会让我遇到之前遇到的相同的“无法找到序号 242”错误,但不修复它将会继续 sklearn 的问题...

最终编辑:通过安装旧版本的 Anaconda 解决。当我能够(2 天)时,将标记为已解决

python scikit-learn scipy anaconda spyder
3个回答
4
投票

我的电脑在无人看管的情况下闲置 4 天后遇到了同样的错误。重启内核解决了。

这可能并不适合每个人,但它可能会减轻一些人的痛苦。


1
投票

我最终通过卸载当前版本的 Anaconda 并安装几个月前的版本来修复此问题。我没有收到“ordinal 242”错误,也没有收到 scikit-learn 的问题。


0
投票

在谷歌协作上有类似的问题,只需卸载并重新安装 scipy 模块就为我解决了这个问题

© www.soinside.com 2019 - 2024. All rights reserved.