【问题标题】:ImportError in importing from sklearn: cannot import name check_build从 sklearn 导入时出现 ImportError:无法导入名称 check_build
【发布时间】:2013-02-22 20:35:57
【问题描述】:

我在尝试从 sklearn 导入时遇到以下错误:

>>> from sklearn import svm

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
   from sklearn import svm
  File "C:\Python27\lib\site-packages\sklearn\__init__.py", line 16, in <module>
   from . import check_build
ImportError: cannot import name check_build

我正在使用 python 2.7、scipy-0.12.0b1 superpack、numpy-1.6.0 superpack、scikit-learn-0.11 我有一台 Windows 7 机器

我已经检查了这个问题的几个答案,但没有一个给出解决这个错误的方法。

【问题讨论】:

  • 尝试将 scikit-learn 更新到当前版本 (0.13)。
  • 现在我收到以下错误
  • ImportError: 无法导入名称 __check_build 而不是 check_build
  • 我确实更新到 scikit 0.13
  • 有人能解决我的问题吗?

标签: python numpy scipy scikit-learn


【解决方案1】:

无需卸载并重新安装 sklearn

试试这个:

from sklearn.model_selection import train_test_split

【讨论】:

    【解决方案2】:

    对我来说, 我通过使用最新的 python 版本(3.7)从新安装 Anaconda 将现有代码升级到新设置 为此,

    from sklearn import cross_validation, 
    from sklearn.grid_search import GridSearchCV
    

    from sklearn.model_selection import GridSearchCV,cross_validate
    

    【讨论】:

      【解决方案3】:

      我有同样的问题,重新安装 anaconda 为我解决了这个问题

      【讨论】:

        【解决方案4】:

        其他答案都不适合我。经过一番修补后,我卸载了 sklearn:

        pip uninstall sklearn
        

        然后我从这里删除了 sklearn 文件夹:(调整你的系统和 python 版本的路径)

        C:\Users\%USERNAME%\AppData\Roaming\Python\Python36\site-packages
        

        并且从这个站点的轮子安装它:link

        该错误可能是因为与安装在其他地方的 sklearn 存在版本冲突。

        【讨论】:

          【解决方案5】:

          我对 Python 3.6.5 64 位 Windows 10 的解决方案:

          1. pip uninstall sklearn
          2. pip uninstall scikit-learn
          3. pip install sklearn

          无需重新启动命令行,但您可以根据需要执行此操作。 我花了一天时间来修复这个错误。希望对您有所帮助。

          【讨论】:

          • 如果您实质上是说最新版本的 sklearn 修复了该错误,那么说明您升级到的 sklearn 版本号会很有帮助。谢谢。
          • 在 Windows 10 中,我还必须在执行这些指令之前启用长路径 stackoverflow.com/questions/54778630/…
          【解决方案6】:

          如果您使用 Anaconda 2.7 64 位,请尝试

          conda upgrade scikit-learn
          

          然后重新启动对我有用的 python shell。

          当我遇到同样的问题并解决它时进行第二次编辑:

          conda upgrade scikit-learn
          

          也适合我

          【讨论】:

            【解决方案7】:

            在窗口中:

            我试图从 shell 中删除 sklearn:pip uninstall sklearn,然后重新安装它但不起作用..

            解决办法:

            1- open the cmd shell.
            2- cd c:\pythonVERSION\scripts
            3- pip uninstall sklearn
            4- open in the explorer: C:\pythonVERSION\Lib\site-packages
            5- look for the folders that contains sklearn and delete them ..
            6- back to cmd: pip install sklearn
            

            【讨论】:

              【解决方案8】:

              从 python.org 安装新的 64 位版本的 Python 3.4 后,我在导入 SKLEARN 时遇到问题。

              原来是 SCIPY 模块坏了,当我尝试“导入 scipy”时,alos 失败了。

              解决方案是卸载 scipy 并使用 pip3 重新安装:

              C:\> pip uninstall scipy
              
              [lots of reporting messages deleted]
              
              Proceed (y/n)? y
                Successfully uninstalled scipy-1.0.0
              
              C:\Users\>pip3 install scipy
              
              Collecting scipy
                Downloading scipy-1.0.0-cp36-none-win_amd64.whl (30.8MB)
                  100% |████████████████████████████████| 30.8MB 33kB/s
              Requirement already satisfied: numpy>=1.8.2 in c:\users\johnmccurdy\appdata\loca
              l\programs\python\python36\lib\site-packages (from scipy)
              Installing collected packages: scipy
              Successfully installed scipy-1.0.0
              
              C:\Users>python
              Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
               on win32
              Type "help", "copyright", "credits" or "license" for more information.
              >>> import scipy
              >>>
              >>> import sklearn
              >>>
              

              【讨论】:

                【解决方案9】:

                我在 Windows 上遇到了同样的问题。按照this answer 的建议,通过从http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy 安装Numpy+MKL 解决了这个问题(建议在其他依赖它的软件包之前安装numpy+mkl)。

                【讨论】:

                  【解决方案10】:

                  安装后numpy,scipy,sklearn还是有错误

                  解决方案:

                  为 Python 设置系统 Path 变量和 PYTHONPATH 环境变量

                  系统变量:将C:\Python34 添加到路径中 用户变量:新增:(name)PYTHONPATH(value)C:\Python34\Lib\site-packages;

                  【讨论】:

                  • 转到 C:\Python34\Lib\site-packages\sklearn 双击 init.py 和 setup.py.
                  【解决方案11】:

                  安装 scipy 后为我工作。

                  【讨论】:

                  • 加上重启python shell。 :)
                  • 检查同一个文件夹中是否有名为“sklearn”的文件。尝试从不同的文件夹运行。为我解决了(我的错)。使用
                  • @Mannu 是的;对我来说,在 jupyter notebook 上,只需重新启动(关闭并再次单击打开)ipynb py-kernel 就可以正常工作,而无需重新启动所有 jupyter notebook。
                  • scipy.stats 也没有工作也引发了错误。就我而言,我在同一个目录中有一个名为multiprocessing.py 的文件,它与内核的套接字连接发生了某种冲突。通过重命名文件multiprocessing_.py(前导下划线不起作用)修复。
                  【解决方案12】:
                  >>> from sklearn import preprocessing, metrics, cross_validation
                  
                  Traceback (most recent call last):
                    File "<pyshell#6>", line 1, in <module>
                      from sklearn import preprocessing, metrics, cross_validation
                    File "D:\Python27\lib\site-packages\sklearn\__init__.py", line 31, in <module>
                      from . import __check_build
                  ImportError: cannot import name __check_build
                  >>> ================================ RESTART ================================
                  >>> from sklearn import preprocessing, metrics, cross_validation
                  >>> 
                  

                  所以,只需尝试重新启动 shell!

                  【讨论】:

                  • 像这样的神奇答案并没有真正的帮助,因为它们不能解决问题。如果重启不能解决问题怎么办?
                  • 只是提到我使用 scikit-learn 0.15.2 针对 numpy 1.9 和 python 2.7 编译时发生了这种情况。
                  • 如果不想重启,del sys.modules['sklearn.__check_build']; import sklearn也可以
                  【解决方案13】:

                  通常当我遇到此类错误时,打开__init__.py 文件并四处寻找会有所帮助。转到目录C:\Python27\lib\site-packages\sklearn 并确保有一个名为__check_build 的子目录作为第一步。在我的机器上(安装了有效的 sklearn、Mac OSX、Python 2.7.3)我有 __init__.pysetup.py、它们关联的 .pyc 文件和二进制文件 _check_build.so

                  查看该目录中的__init__.py,下一步我将转到sklearn/__init__.py 并注释掉import 语句——check_build 内容只是检查编译是否正确,它没有除了调用预编译的二进制文件之外,它似乎什么都做。当然,这需要您自担风险,并且(可以肯定)是一种解决方法。如果您的构建失败,您可能很快就会遇到其他更大的问题。

                  【讨论】:

                  • 我的文件夹中没有 _check_build.so,我该怎么做才能为 python 2.7 获取这个
                  • 您是否尝试注释掉试图在sklearn/__init__.py 中导入它的行?
                  • 目前,您可以注释掉执行导入的行,这应该可以让您运行。能否请您在问题跟踪器中打开一个问题github.com/scikit-learn/scikit-learn/issues 看起来像一个错误。
                  • FWIW 在__init__.py 中注释掉import 并没有解决问题,但确实暴露了一个不同的错误——我的scipy 安装没有成功。问题中的错误掩盖了真正的问题
                  猜你喜欢
                  • 2020-07-21
                  • 2016-01-16
                  • 2017-03-23
                  • 2020-06-04
                  • 2016-12-02
                  • 2019-05-27
                  • 2022-01-21
                  相关资源
                  最近更新 更多