Importerror: cannot import name ‘joblib’ from ‘sklearn.externals’

Working with Python project, chances to face Importerror: cannot import name ‘joblib’ from ‘sklearn.externals error is inevitable.

So this article aims to address the causes and impacts of this error on your code and to provide multiple solutions to fix it.

Particularly, solutions range from upgrading Scikit-learn to installing ‘joblib’ separately, changing the import statement, and downgrading ‘joblib’.

By the end of this article, you’ll have a comprehensive understanding about this error and how to resolve it in your code.”

What is Importerror: cannot import name ‘joblib’ from ‘sklearn.externals’?

The “ImportError: cannot import name ‘joblib’ from ‘sklearn. externals‘” usually occurs when you try to import the ‘joblib’ module from the ‘sklearn.externals’ package in a Python script or Jupyter notebook.

Here is how this error occurs:

from sklearn.externals import joblib

model = joblib.load('model.pkl')

Output:

Traceback (most recent call last):
  File "C:\Users\Windows\PycharmProjects\pythonProject1\main.py", line 1, in <module>
    from sklearn.externals import joblib
ImportError: cannot import name 'joblib' from 'sklearn.externals' (C:\Users\Windows\PycharmProjects\pythonProject1\venv\lib\site-packages\sklearn\externals\__init__.py)

To understand why the error occurs, the code tries to import ‘joblib’ from the ‘sklearn.externals’ package.

Then load a trained model from a pickle file using the ‘joblib.load()’ method.

However, this results in “ImportError: cannot import name ‘joblib’ from ‘sklearn.externals'” error because ‘joblib’ has been moved to the ‘sklearn.utils’ package in newer versions of Scikit-learn.

Solutions Importerror: cannot import name ‘joblib’ from ‘sklearn.externals’

Since we already the causes and how this error occurs, here are the solutions to fix the Importerror: cannot import name ‘joblib’ from ‘sklearn.externals’.

Upgrade Scikit-learn

If you are using an older version of Scikit-learn (version 0.22 or earlier), you can upgrade to a newer version (version 0.23 or later) where ‘joblib’ has been moved to the ‘sklearn.utils’ package.

To do this, run the following command in your terminal or command prompt:

pip install --upgrade scikit-learn

Install joblib separately

If you don’t want to upgrade Scikit-learn, you can install ‘joblib’ separately using pip.

To do this, run the following command in your terminal or command prompt:

pip install joblib

Change the import statement

Instead of importing ‘joblib’ from ‘sklearn.externals’, you can import it from ‘sklearn.utils’ which is where it is located in newer versions of Scikit-learn.

To do this, replace:

from sklearn.externals import joblib

with this import statement:

from sklearn.utils import joblib

Downgrade joblib

If you are using a version of Scikit-learn that requires ‘joblib’ to be imported from ‘sklearn.externals’, you can try downgrading ‘joblib’ to an earlier version.

To do this, run the following command in your terminal or command prompt:

pip install joblib==0.14.1

Try these solutions one by one until it will work best for you.

Here is an example code where joblib is working:

import joblib
from sklearn.ensemble import RandomForestClassifier

X = [[19, 19], [21, 21]]
y = [19, 21]
clf = RandomForestClassifier()
clf.fit(X, y)

joblib.dump(clf, 'model.pkl')

loaded_clf = joblib.load('data_model.pkl')

predictions = loaded_clf.predict([[2, 2]])
print(predictions)

Output:

[19]

It will create a data_model.pkl file in your current working directory.

Anyway, here are other fixed errors you can consider when somehow you might encounter them.

Conclusion

In conclusion, Importerror: cannot import name ‘joblib’ from ‘sklearn.externals’ occurs when attempting to import ‘joblib’ from the ‘sklearn.externals’ package, which is no longer supported in newer versions of Scikit-learn.

So by following the given solutions above, surely it will fix the error.

I think that’s all for this error. I hope this article has helped you fix it.

Until next time! 😊