Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import scikit-learn in python virtual environment?

The Problem

I'm trying to use scikit-learn on a virtual environment and I cannot get it to run.

So, for example, I go to a project folder and install NumPy, SciPy, and scikit-learn

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

Then I open python and try to import each of them.

import numpy import scipy import sklearn

NumPy and SciPy import fine, but when I try to import I get an error saying Library not loaded: @rpath/./libgfortran.3.dylib.

Is there something I'm missing in the installation?

Full error message: 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

EDIT: this was some troubleshooting to host a Heroku app using sklearn.

like image 481
plfrick Avatar asked Dec 01 '25 03:12

plfrick


1 Answers

I would strongly recommend against using virtual environments for NumPy/SciPy/Scikit-learn and other packages which include compiled code. Virtual environments deal with pure Python dependencies reasonably well, but for compiled extensions with dependencies outside Python, they can fail in unexpected ways (as you have seen).

If you want to manage multiple environments with different versions of Python packages including compiled extensions, the best tool out there is probably conda, and in particular conda environments.

With it, you can run the following:

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

and you'll be good to go.

like image 152
jakevdp Avatar answered Dec 04 '25 02:12

jakevdp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!