sklearn 导入错误:没有名为 _check_build 的模块

13 投票
7 回答
32138 浏览
提问于 2025-04-18 02:41

我在尝试导入sklearn的时候,遇到了以下问题:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-8fd979e02004> in <module>()
----> 1 import sklearn

C:\Users\Alpine\AppData\Local\Enthought\Canopy\User\sklearn\__init__.py in <module>()
     29     # process, as it may not be compiled yet
     30 else:
---> 31     from . import __check_build
     32     from .base import clone
     33 

C:\Users\Alpine\AppData\Local\Enthought\Canopy\User\sklearn\__check_build\__init__.py in <module>()
     44     from ._check_build import check_build
     45 except ImportError as e:
---> 46     raise_build_error(e)

C:\Users\Alpine\AppData\Local\Enthought\Canopy\User\sklearn\__check_build\__init__.py in raise_build_error(e)
     39 to build the package before using it: run `python setup.py install` or
     40 `make` in the source directory.
---> 41 %s""" % (e, local_dir, ''.join(dir_content).strip(), msg))
     42 
     43 try:

ImportError: No module named _check_build
___________________________________________________________________________
Contents of C:\Users\Alpine\AppData\Local\Enthought\Canopy\User\sklearn\__check_build:
setup.py                  setup.pyc                 _check_build.c
_check_build.pyx          __init__.py               __init__.pyc
___________________________________________________________________________
It seems that scikit-learn has not been built correctly.

If you have installed scikit-learn from source, please do not forget
to build the package before using it: run `python setup.py install` or
`make` in the source directory.

If you have used an installer, please check that it is suited for your
Python version, your operating system and your platform.

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line string', (1, 2))

我很确定scikit-learn 已经正确安装,因为我成功运行了 python setup.py install,没有出现任何问题。不过,我尝试运行 python setup.py bdist_wininst -b doc/logos/scikit-learn-logo.bmp 时,被中断了,出现了 error: cannot copy tree 'build\scripts.win-amd64-2.7': not a directory 的错误。

有没有人知道怎么解决这个问题吗?

7 个回答

0

确保你使用的是最新的Python版本来运行代码。如果你的电脑上安装了多个Python版本,可以把第一行 "#!/usr/bin/python" 删除,然后用最新的Python版本的完整路径来运行,比如 "/usr/local/bin/python3.4 '文件名'"。如果你的Python版本低于2.7,建议你升级到更高的版本,并安装所需的依赖,然后再试试上面的方法。

0

这个内容不完全符合你的问题,因为它跟Enthought的分发没有关系;不过,我遇到过一个类似的问题,涉及到一个使用了sklearn的模块。错误信息大概是这样的:

$ python -m somefile.py           // somefile.py uses sklearn at some point
Cannot import name __check_build

然后我从这个页面安装了scikit-learn(还有scipynumpy-MKL),问题似乎就解决了。

2

避免这个错误的一种方法是确保你安装了所有需要的依赖项。在Ubuntu或Debian系统上,你可以通过运行以下命令来做到这一点:

sudo apt-get build-dep python-sklearn
3

好的,下面的内容对我来说是有效的 :)

sudo make
sudo python setup.py install

最后,来测试一下安装是否成功:

nosetests --exe sklearn
9

我在Windows 10上也遇到了同样的问题。我安装了带有Python 3.7的Anaconda,这个安装不仅带来了这个问题。要解决这个问题,可以在Anaconda命令提示符中运行以下命令:

conda install scikit-learn

至少这个方法对我有效。

撰写回答