Joe Antognini
Joe Antognini

Reputation: 243

Scikit-learn fails to import only in Jupyter notebook

I have Anaconda installed on OS X. I am able to import sklearn from a python terminal and an IPython terminal. But when I try to import sklearn from a Jupyter notebook, I get the following error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-8fd979e02004> in <module>()
----> 1 import sklearn

/Users/joe/anaconda/envs/data_env/lib/python3.5/site-packages/sklearn/__init__.py in <module>()
     55 else:
     56     from . import __check_build
---> 57     from .base import clone
     58     __check_build  # avoid flakes unused variable error
     59 

/Users/joe/anaconda/envs/data_env/lib/python3.5/site-packages/sklearn/base.py in <module>()
     10 from scipy import sparse
     11 from .externals import six
---> 12 from .utils.fixes import signature
     13 from .utils.deprecation import deprecated
     14 from .exceptions import ChangedBehaviorWarning as _ChangedBehaviorWarning

/Users/joe/anaconda/envs/data_env/lib/python3.5/site-packages/sklearn/utils/__init__.py in <module>()
      9 
     10 from .murmurhash import murmurhash3_32
---> 11 from .validation import (as_float_array,
     12                          assert_all_finite,
     13                          check_random_state, column_or_1d, check_array,

/Users/joe/anaconda/envs/data_env/lib/python3.5/site-packages/sklearn/utils/validation.py in <module>()
     16 
     17 from ..externals import six
---> 18 from ..utils.fixes import signature
     19 from .deprecation import deprecated
     20 from ..exceptions import DataConversionWarning as _DataConversionWarning

/Users/joe/anaconda/envs/data_env/lib/python3.5/site-packages/sklearn/utils/fixes.py in <module>()
    288     from ._scipy_sparse_lsqr_backport import lsqr as sparse_lsqr
    289 else:
--> 290     from scipy.sparse.linalg import lsqr as sparse_lsqr
    291 
    292 

/Users/joe/anaconda/envs/data_env/lib/python3.5/site-packages/scipy/sparse/linalg/__init__.py in <module>()
    110 from __future__ import division, print_function, absolute_import
    111 
--> 112 from .isolve import *
    113 from .dsolve import *
    114 from .interface import *

/Users/joe/anaconda/envs/data_env/lib/python3.5/site-packages/scipy/sparse/linalg/isolve/__init__.py in <module>()
      4 
      5 #from info import __doc__
----> 6 from .iterative import *
      7 from .minres import minres
      8 from .lgmres import lgmres

/Users/joe/anaconda/envs/data_env/lib/python3.5/site-packages/scipy/sparse/linalg/isolve/iterative.py in <module>()
      5 __all__ = ['bicg','bicgstab','cg','cgs','gmres','qmr']
      6 
----> 7 from . import _iterative
      8 
      9 from scipy.sparse.linalg.interface import LinearOperator

ImportError: dlopen(/Users/joe/anaconda/envs/data_env/lib/python3.5/site-packages/scipy/sparse/linalg/isolve/_iterative.so, 2): Library not loaded: /usr/local/lib/libgcc_s.1.dylib
  Referenced from: /Users/joe/anaconda/envs/data_env/lib/python3.5/site-packages/scipy/sparse/linalg/isolve/_iterative.so
  Reason: image not found

I can import numpy, scipy, and pandas fine from the Jupyter notebook. It is just sklearn that fails.

I have also tried creating a new conda environment (conda create -n test_env jupyter notebook matplotlib scipy numpy pandas scikit-learn), but the error persists in the new environment as well.

Upvotes: 0

Views: 1213

Answers (1)

Joe Antognini
Joe Antognini

Reputation: 243

I managed to figure out what was going on, so I'll post my solution here in case anyone else runs into the same problem. As it turns out, I had modified the DYLD_FALLBACK_LIBRARY_PATH environment variable in my .bashrc file when I had installed another piece of software. Restoring this environment variable to its default fixed the problem for me.

(Incidentally, scikit-learn was failing to import in a standard Python terminal as well. I didn't initially realize this because I was testing the Python terminal in an environment in which I had accidentally restored the environment variables to their defaults, overwriting the change I had made in my .bashrc file.)

Upvotes: 2

Related Questions