How to fix “ImportError: cannot import name ‘joblib’ from ‘sklearn.externals’ “

When working with Python’s famous machine learning tool scikit-learn, facing import problems can be a stressful roadblock. One common issue arises when trying to import joblib from sklearn.externals with the error message: ImportError: cannot import name ‘joblib’ from ‘sklearn.externals’.

In this article, we are going to understand and resolve the error: How to fix “ImportError: cannot import name ‘joblib’ from ‘sklearn.externals’ “.

What is Importerror: “cannot import name ‘joblib” in Python?

The joblib module is widely used for saving and loading Python objects that require significant amounts of data, both more efficiently and effectively than Python’s built-in alternatives. It has often been bundled with scikit-learn, particularly in older versions, as it is integral for models that involve heavy I/O operations. However, starting with scikit-learn version 0.21.0, joblib was no longer included under sklearn.externals and was required to be installed separately. This problem stems from changes in the scikit-learn library that have removed certain paths and methods, forcing adjustments in how libraries are loaded.

How this error Generates?

This error generate due to import joblib from sklearn.externals in a version of scikit-learn where this is no longer supported (version 0.21.0 or later).

Example:

Python
from sklearn.externals import joblib
# Define a simple model
model = {'name': 'Example Model'}
# save and load the model
joblib.dump(model, 'example_model.pkl')
loaded_model = joblib.load('example_model.pkl')
print(loaded_model)

Output:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-12-5d1af3daf445> in <cell line: 1>()
----> 1 from sklearn.externals import joblib
      2 
      3 # Define a simple model
      4 model = {'name': 'Example Model'}
      5 

ImportError: cannot import name 'joblib' from 'sklearn.externals' (/usr/local/lib/python3.10/dist-packages/sklearn/externals/__init__.py)

How to Resolve the ImportError: cannot import name ‘joblib’ from ‘sklearn.externals’?

Methods 1: Install joblib separately (if not already installed):

You can install joblib using pip. Open your command prompt or terminal and run:

pip install joblib

Update your import statement:

Replace the import statement in your Python script or notebook from:

from sklearn.externals import joblib

to:

import joblib

This change should fix the import error. Ensure that your environment is using a compatible version of scikit-learn and that joblib is installed correctly. If you encounter any further issues, you may need to verify the versions of the libraries you are using and adjust them accordingly.

Methods 2: Install Missing Dependencies

Use the appropriate package manager (e.g., pip) to install any missing dependencies. Make sure that all required packages are installed and up-to-date.

pip install required_package