Scikit-learn fails to import only in Jupyter notebook - python

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.

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.)

Related

How do I resove the ImportError encountered while using the fssa package in python?

I am using python from a anaconda installation and using the python
notebook. From the jupyer notebook (ipython) I import fssa like:
import fssa
I get the following error message (see below).
Any help from you will be appreciated.
I have tried uninstalling and reinstalling scipy, numpy, and fssa and reinstalling
them, but the problem seems to remain.
ImportError Traceback (most recent call last)
/tmp/ipykernel_218856/781571052.py in <module>
----> 1 import fssa
~/anaconda3/lib/python3.9/site-packages/fssa/__init__.py in <module>
82
83 import pkg_resources
---> 84 from .fssa import scaledata, quality, autoscale
85
86 __version__ = pkg_resources.get_distribution(__name__).version
~/anaconda3/lib/python3.9/site-packages/fssa/fssa.py in <module>
46 import scipy.optimize
47
---> 48 from .optimize import _minimize_neldermead
49
50
~/anaconda3/lib/python3.9/site-packages/fssa/optimize.py in <module>
11 import numpy
12 from numpy import asfarray
---> 13 from scipy.optimize.optimize import (OptimizeResult,
14 _status_message, wrap_function)
15
ImportError: cannot import name 'wrap_function' from 'scipy.optimize.optimize'
(/home/debasish/anaconda3/lib/python3.9/site-packages/scipy/optimize/optimize.py)

TypeError: an integer is required (got type bytes) when importing sklearn

import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import matplotlib.image as pltimg
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
y_pred = dtree.predict(X)
y_pred
The error ::
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-745bbfa9769b> in <module>
1 import pandas
----> 2 from sklearn import tree
3 import pydotplus
4 from sklearn.tree import DecisionTreeClassifier
5 import matplotlib.pyplot as plt
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/__init__.py in <module>
62 else:
63 from . import __check_build
---> 64 from .base import clone
65 from .utils._show_versions import show_versions
66
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/base.py in <module>
12 from scipy import sparse
13 from .externals import six
---> 14 from .utils.fixes import signature
15 from .utils import _IS_32BIT
16 from . import __version__
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/__init__.py in <module>
12 from .murmurhash import murmurhash3_32
13 from .class_weight import compute_class_weight, compute_sample_weight
---> 14 from . import _joblib
15 from ..exceptions import DataConversionWarning
16 from .fixes import _Sequence as Sequence
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/_joblib.py in <module>
20 from joblib import parallel_backend, register_parallel_backend
21 else:
---> 22 from ..externals import joblib
23 from ..externals.joblib import logger
24 from ..externals.joblib import dump, load
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/__init__.py in <module>
117 from .numpy_pickle import load
118 from .compressor import register_compressor
--> 119 from .parallel import Parallel
120 from .parallel import delayed
121 from .parallel import cpu_count
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/parallel.py in <module>
26 from .my_exceptions import TransportableException
27 from .disk import memstr_to_bytes
---> 28 from ._parallel_backends import (FallbackToBackend, MultiprocessingBackend,
29 ThreadingBackend, SequentialBackend,
30 LokyBackend)
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/_parallel_backends.py in <module>
20 from .pool import MemmappingPool
21 from multiprocessing.pool import ThreadPool
---> 22 from .executor import get_memmapping_executor
23
24 # Compat between concurrent.futures and multiprocessing TimeoutError
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/executor.py in <module>
12 from .disk import delete_folder
13 from ._memmapping_reducer import get_memmapping_reducers
---> 14 from .externals.loky.reusable_executor import get_reusable_executor
15
16
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/loky/__init__.py in <module>
10
11 from .backend.context import cpu_count
---> 12 from .backend.reduction import set_loky_pickler
13 from .reusable_executor import get_reusable_executor
14 from .cloudpickle_wrapper import wrap_non_picklable_objects
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/loky/backend/reduction.py in <module>
123 # global variable to change the pickler behavior
124 try:
--> 125 from sklearn.externals.joblib.externals import cloudpickle # noqa: F401
126 DEFAULT_ENV = "cloudpickle"
127 except ImportError:
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/cloudpickle/__init__.py in <module>
1 from __future__ import absolute_import
2
----> 3 from .cloudpickle import *
4
5 __version__ = '0.6.1'
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py in <module>
165
166
--> 167 _cell_set_template_code = _make_cell_set_template_code()
168
169
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/externals/joblib/externals/cloudpickle/cloudpickle.py in _make_cell_set_template_code()
146 )
147 else:
--> 148 return types.CodeType(
149 co.co_argcount,
150 co.co_kwonlyargcount,
TypeError: an integer is required (got type bytes)
So I keep getting this type error and have no clue what to do pretty sure that sklearn is installed. Both X and y are previously defined after creating the training set to be used. I've tried reinstalling scikit-learn, downgrading python and installing through the terminal but none of that helped. Just cant figure out what the issue is here and will really appreciate some help on this
I was able to reproduce the problem in a conda environment by running conda install python==3.8 scikit-learn but then manually downgrading joblib by running pip install joblib<0.14 since joblib<0.14 is incompatible with Python 3.8.
I'm not exactly sure how you got into the this situation, but it should fix it to first uninstall any joblib package that might have been mis-installed:
$ pip uninstall joblib
Then force reinstall/upgrade it with conda:
$ conda update --force-reinstall joblib
Confirm the correct version was installed:
$ python -c 'import joblib; print(joblib.__version__)'
0.17.0
If all else fails, try creating a fresh Anaconda install. Make sure to install packages with conda install and not pip.
In my case, joblib in scikit needs to be reinstalled.
pip uninstall joblib
This happened to me because I pip installed sklearn instead of scikit-learn.
In my case, I did
pip uninstall sklearn --yes
followed by
pip install sklearn

how to correct ImportError: cannot import name 'murmurhash3_32'

I installed scikit-learn library in python using the command
pip install -U scikit-learn
When I am trying to import the library or it's module like
from sklearn.model_selection import train_test_split
or simply import sklearn
I am getting the error
ImportError Traceback (most recent call last)
<ipython-input-24-73edc048c06b> in <module>()
----> 1 from sklearn.model_selection import train_test_split
c:\users\ajain9\appdata\local\programs\python\python36-32\lib\site-packages\sklearn\__init__.py in <module>()
132 else:
133 from . import __check_build
--> 134 from .base import clone
135 __check_build # avoid flakes unused variable error
136
c:\users\ajain9\appdata\local\programs\python\python36-32\lib\site-packages\sklearn\base.py in <module>()
11 from scipy import sparse
12 from .externals import six
---> 13 from .utils.fixes import signature
14 from . import __version__
15
c:\users\ajain9\appdata\local\programs\python\python36-32\lib\site-packages\sklearn\utils\__init__.py in <module>()
7 import warnings
8
----> 9 from .murmurhash import murmurhash3_32
10 from .validation import (as_float_array,
11 assert_all_finite,
ImportError: cannot import name 'murmurhash3_32'
Any reason this error might be happening?
I am using Python version 3.6.3 Numpy v 1.13.3 pandas v 0.21.0
I am using windows
Try using virutalenv and install all the libraries required there, it worked for me.

scikit-learn error: libquadmath.so.0: cannot open shared object file: No such file or directory

I got this erreur when I've tried to import scikit learn to my python 3.5 (jupyter notebook):
from sklearn.model_selection import train_test_split
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-139cc3505715> in <module>()
----> 1 from sklearn.model_selection import train_test_split
/opt/Anaconda/Anaconda3-4.2.0/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
/opt/Anaconda/Anaconda3-4.2.0/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
/opt/Anaconda/Anaconda3-4.2.0/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,
/opt/Anaconda/Anaconda3-4.2.0/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
/opt/Anaconda/Anaconda3-4.2.0/lib/python3.5/site-packages/sklearn/utils/fixes.py in <module>()
289 from ._scipy_sparse_lsqr_backport import lsqr as sparse_lsqr
290 else:
--> 291 from scipy.sparse.linalg import lsqr as sparse_lsqr
292
293
/opt/Anaconda/Anaconda3-4.2.0/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 *
/opt/Anaconda/Anaconda3-4.2.0/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
/opt/Anaconda/Anaconda3-4.2.0/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: libquadmath.so.0: cannot open shared object file: No such file or directory
the scikit learn 0.18.2 package is already installed and I can see it on the list when I run conda list(I got the same error with the last version 0.19.1).
I work with the Anaconda 3 (conda 4.2.2)
you'r python installation has missing dynamically linked shared library which is used by scipy.
generally when you install anaconda under lib directory this shared objects are present and respective paths are exported in environment for dynamic linking.
check if this files are there under the lib directory of your anaconda installation.
e.g:-
/anaconda3/lib/libquadmath.0.dylib
if it is not there then please reinstall anaconda it will solve your problem.
if this file exists then please check your environment path. which must have anaconda installation path in there list.

import pyplot, RuntimeError: Cannot dlopen tkinter module file

I used pyenv to install pypy in windows bash, then use pip to install matplotlib, but when importing pyplot, I got a RunTimeError
In [1]: import matplotlib.pyplot as plt
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-1-eff513f636fd> in <module>()
----> 1 import matplotlib.pyplot as plt
/home/zjshao/.pyenv/versions/pypy-5.3.1/site-packages/matplotlib/pyplot.pyc in <module>()
112
113 from matplotlib.backends import pylab_setup
--> 114 _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
115
116 _IP_REGISTERED = None
/home/zjshao/.pyenv/versions/pypy-5.3.1/site-packages/matplotlib/backends/__init__.pyc in pylab_setup()
30 # imports. 0 means only perform absolute imports.
31 backend_mod = __import__(backend_name,
---> 32 globals(),locals(),[backend_name],0)
33
34 # Things we pull in from all backends
/home/zjshao/.pyenv/versions/pypy-5.3.1/site-packages/matplotlib/backends/backend_tkagg.pyc in <module>()
11
12 # Paint image to Tk photo blitter extension
---> 13 import matplotlib.backends.tkagg as tkagg
14
15 from matplotlib.backends.backend_agg import FigureCanvasAgg
/home/zjshao/.pyenv/versions/pypy-5.3.1/site-packages/matplotlib/backends/tkagg.pyc in <module>()
7 import numpy as np
8
----> 9 from matplotlib.backends import _tkagg
10
11 def blit(photoimage, aggimage, bbox=None, colormode=1):
RuntimeError: Cannot dlopen tkinter module file
In [2]:
So what can be the problem, and how to solve this problem?
Any suggestions would be appreciated!
Perhaps something wrong with your python installation. The best ways is delete python and reinstall.
I suggest you install python from Anaconda. It is 'next-click' installer (awesome for windows) which set python path environment variable, and have useful package pre-installed, including 'mathplotlib'.

Categories

Resources