'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



Solution 1:[1]

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.

Solution 2:[2]

In my case, joblib in scikit needs to be reinstalled.

pip uninstall joblib

Solution 3:[3]

This happened to me because I pip installed sklearn instead of scikit-learn.

Solution 4:[4]

In my case, I did

pip uninstall sklearn --yes

followed by

pip install sklearn

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Iguananaut
Solution 2 SulerBK
Solution 3 Muriel
Solution 4 sam