[Solved] Importerror: cannot import name joblib from sklearn.externals

Sometimes when you try to import sklearn.externals, you get importerror: cannot import name joblib from sklearn.externals error. There can be many reasons for getting this error. In this short article, we will learn how we can solve importerror: cannot import name joblib from sklearn.externals error using various methods. Moreover, we will discuss why we are getting this error as well.

Importerror: cannot import name joblib from sklearn.externals – Possible solutions

The Importerror: cannot import name joblib from sklearn.externals error occurs because of the absence of the joblib module in sklearn.externals. One of the simplest ways to solve the error is to directly import the joblib module rather than importing if from the sklearn.externals.

Let us now try some methods to solve the importerror: cannot import name joblib from sklearn.externals error. Here we will discuss various methods through which you can solve the issue:

importerror: cannot import name joblib from sklearn.externals

The error can be solved by the following methods:

  • Directly import joblib
  • Upgrade the sklearn module
  • Alternative methods
  • Upgrade all requirements

Method-1: Directly import joblib

If you are getting this error because you are using sklearn.external and tried to import joblib from there. Then why not directly import joblib rather than importing it from sklearn.external.

Here is how you can directly import joblib module:

import joblib

If you get an error that no module named joblib is found then it is because you have not installed the module.

Depending on your system, you can choose any of the following methods to install the joblib module on your system.

# upgrading the joblib module
pip install joblib --upgrade
pip3 install joblib --upgrade

# if you don't have pip in PATH environment variable
python -m pip install joblib --upgrade
python3 -m pip install joblib --upgrade

# py alias only for windows
py -m pip install joblib --upgrade

#In case you are using Anaconda
conda install -c anaconda joblib

#Run the following command in cell of Jupyter Notebook
!pip install joblib --upgrade

Hopefully, you will not get the error anymore.

Method-2: Upgrade the sklearn module

If you want to use the joblib only from the sklearn but when you tried to import, you get the error. Then you can try upgrading the sklearn module. Use the following commands to upgrade the sklearn module depending on the system you are using:

# upgrading sklearn using pip command
pip install scikit-learn --upgrade

# upgrade sklearn module using pip3 command
pip3 install scikit-learn --upgrade

# if you don't have pip in PATH environment variable
# then you can use the following commands
python -m pip install scikit-learn --upgrade
python3 -m pip install scikit-learn --upgrade

#  alias method only fow windows
py -m pip install scikit-learn --upgrade

# In case is you are using Anaconda
conda install -c conda-forge scikit-learn

# for Jupyter Notebook
!pip install scikit-learn --upgrade

Hopefully, now you will be able to use the sklearn.externals and will be able to import joblib module.

Method-3: Alternative method

If none of the above-mentioned methods works for you, then you can try using the following command where you need to import sklearn and joblib separately.

#importing skearn and joblib modules
import joblib
import sklearn

# setting the joblib 
sklearn.externals.joblib = joblib

The code sample sets the externals.joblib attribute to the actual joblib module, so no error is raised when sklearn.externals.joblib is accessed.

Method-4: Upgrade all required modules

There can be other reasons for this error as well. Sometimes, because of the difference in the versions of different modules, it is possible to get errors. In such cases, we can simply upgrade all the required modules by running the following commands:

# importing the modules
import pkg_resources
from subprocess import call

# upgrading the packages
packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade " + ' '.join(packages), shell=True)

Here is another command using which you can upgrade all modules.

# use this command for macOS or Linux
pip install -U `pip list --outdated | awk 'NR>2 {print $1}'`

# use this command for Windows
for /F "delims= " %i in ('pip list --outdated') do pip install -U %i

Hopefully, now you will not get the error. Cheer!!!!

Understanding the Importerror: cannot import name joblib from sklearn.externals error

Let us now try to understand errors in Python taking the Importerror: cannot import name joblib from sklearn.externals error as an example. In Python, the errors have two main parts which give us enough information to solve the error. The first part of the error shows the category of the error which in our case is the ImportError which means there has been a problem in importing a module.

The second part of the error gives more specific information about the error and tells us which module was not imported. In our case the module that we have not imported is joblib.

Reasons for getting error

The “ImportError: cannot import name ‘joblib’ from ‘sklearn.externals'” error message can be caused by one or more of the following reasons:

  1. Version incompatibility: If you are using an older version of scikit-learn, it may not include the joblib module. In this case, you can try updating your scikit-learn version to the latest one that includes joblib.
  2. Missing dependencies: It’s possible that you have installed scikit-learn without installing its dependencies. Make sure that you have installed all the necessary dependencies, including joblib.
  3. Incorrect module name: Double-check that you are importing the correct module name. The correct import statement should be “from sklearn.externals import joblib”.
  4. Path issues: The path to the scikit-learn package or the joblib module may not be set correctly. You can try adding the path to your package or module to the system path or specifying the path explicitly when importing.
  5. Installation issues: There may have been an issue with the installation of the scikit-learn package or joblib module. In this case, you can try uninstalling and reinstalling both packages.
  6. Namespace conflicts: It’s possible that another package or module with the same name as joblib is installed on your system, causing a conflict. In this case, you can try renaming the conflicting package or module or specifying the full path to the correct module when importing.

Summary

In this short article, we learned how to solve importerror: cannot import name joblib from sklearn.externals error. We solved the error using 4 different methods and hopefully, any one will help you to solve the error on your system.

Further Reading

You may also read:

3 thoughts on “[Solved] Importerror: cannot import name joblib from sklearn.externals”

  1. Pingback: [Fixed] Typeerror: 'float' object cannot be interpreted as an integer - Techfor-Today

  2. Pingback: SyntaxError: Multiple Statements Found While Compiling a Single Statement - Techfor-Today

  3. Pingback: [Solved] Importerror: DLL load failed: the specified procedure could not be found - Techfor-Today

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top