无法在python中导入sklearn

时间:2016-02-03 11:27:03

标签: python python-2.7 scikit-learn

from sklearn import svm

导入sklearn模块时出现以下错误。

/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/isolve/iterative.py in <module>()
  9 
 10 from scipy.sparse.linalg.interface import LinearOperator
---> 11 from scipy.lib.decorator import decorator
 12 from .utils import make_system
 13 

ImportError: No module named decorator


ImportError                               Traceback (most recent call last)
<ipython-input-6-e938be4cf50b> in <module>()
----> 1 from sklearn import svm

/usr/local/lib/python2.7/dist-packages/sklearn/__init__.py in <module>()
     54     # process, as it may not be compiled yet
     55 else:
---> 56     from . import __check_build
     57     from .base import clone
     58     __check_build  # avoid flakes unused variable error

ImportError: cannot import name __check_build

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我之前遇到过相同的错误,原因是机器没有安装scipy软件包,或者分发错误(取决于架构或操作系统)。

如果您的计算机中有pip,请尝试使用以下方法安装scipy:

sudo pip install scipy

如果您已经尝试升级或重新安装软件包。

修改

按照@erip建议的解决方案,您可以创建一个虚拟环境并测试解决方案,保持您的python全局site-packages目录清洁:

virtualenv test # This creates a folder with the virtual env
source test/bin/activate
pip install scipy
pip install sklearn

停用并删除虚拟环境:

deactivate
rm -r test # Remove the folder