如何在python虚拟环境中导入scikit学习



问题

我正在尝试在虚拟环境中使用scikit学习,但我无法让它运行。

例如,我转到一个项目文件夹,安装NumPy、SciPy和scikit学习

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应用程序的一些疑难解答。

我强烈建议不要使用NumPy/SciPy/Scikit学习的虚拟环境和其他包含编译代码的包。虚拟环境可以很好地处理纯Python依赖项,但对于在Python之外具有依赖项的已编译扩展,它们可能会以意想不到的方式失败(正如您所看到的)。

如果你想用不同版本的Python包(包括编译的扩展)管理多个环境,最好的工具可能是conda,尤其是conda环境。

有了它,你可以运行以下程序:

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

你就可以走了。

相关内容

  • 没有找到相关文章

最新更新