如何在python虚拟环境中导入scikit-learn?

时间:2015-11-18 23:30:49

标签: python-2.7 heroku scikit-learn python-venv

问题

我正在尝试在虚拟环境中使用scikit-learn而我无法让它运行。

因此,例如,我转到项目文件夹并安装NumPy,SciPy和scikit-learn

virtualenv venv source venv/bin/activate pip install numpy pip install scipy pip install scikit-learn

然后我打开python并尝试导入它们。

import numpy import scipy import sklearn

NumPy和SciPy导入正常,但是当我尝试导入时,我收到错误Library not loaded: @rpath/./libgfortran.3.dylib

安装中是否有遗漏的东西?

完整的错误讯息: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/Peter/git/dataSandbox/flaskApps/sfSalary/venv/lib/python2.7/site-packages/sklearn/__init__.py", line 57, in <module> from .base import clone File "/Users/Peter/git/dataSandbox/flaskApps/sfSalary/venv/lib/python2.7/site-packages/sklearn/base.py", line 11, in <module> from .utils.fixes import signature File "/Users/Peter/git/dataSandbox/flaskApps/sfSalary/venv/lib/python2.7/site-packages/sklearn/utils/__init__.py", line 11, in <module> from .validation import (as_float_array, File "/Users/Peter/git/dataSandbox/flaskApps/sfSalary/venv/lib/python2.7/site-packages/sklearn/utils/validation.py", line 16, in <module> from ..utils.fixes import signature File "/Users/Peter/git/dataSandbox/flaskApps/sfSalary/venv/lib/python2.7/site-packages/sklearn/utils/fixes.py", line 324, in <module> from scipy.sparse.linalg import lsqr as sparse_lsqr File "/Users/Peter/git/dataSandbox/flaskApps/sfSalary/venv/lib/python2.7/site-packages/scipy/sparse/linalg/__init__.py", line 109, in <module> from .isolve import * File "/Users/Peter/git/dataSandbox/flaskApps/sfSalary/venv/lib/python2.7/site-packages/scipy/sparse/linalg/isolve/__init__.py", line 6, in <module> from .iterative import * File "/Users/Peter/git/dataSandbox/flaskApps/sfSalary/venv/lib/python2.7/site-packages/scipy/sparse/linalg/isolve/iterative.py", line 7, in <module> from . import _iterative ImportError: dlopen(/Users/Peter/git/dataSandbox/flaskApps/sfSalary/venv/lib/python2.7/site-packages/scipy/sparse/linalg/isolve/_iterative.so, 2): Library not loaded: @rpath/./libgfortran.3.dylib Referenced from: /Users/Peter/git/dataSandbox/flaskApps/sfSalary/venv/lib/python2.7/site-packages/scipy/sparse/linalg/isolve/_iterative.so Reason: image not found

编辑: 这是使用sklearn托管Heroku应用程序的一些故障排除。

1 个答案:

答案 0 :(得分:2)

我强烈建议反对使用虚拟环境进行NumPy / SciPy / Scikit-learn和其他包含编译代码的软件包。虚拟环境可以很好地处理纯Python依赖关系,但是对于在Python之外具有依赖关系的编译扩展,它们可能以意想不到的方式失败(如您所见)。

如果您想管理包含不同版本Python包的多个环境(包括已编译的扩展程序),那么最好的工具可能是conda,特别是conda environments

有了它,您可以运行以下命令:

$ conda create -n myenv python=3.4 numpy scipy scikit-learn
$ source activate myenv
(myenv) $ python
Python 3.4.3
>>> import sklearn

你会很高兴。