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:

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 an as an example. In Python, the errors have two main parts which gives 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 not imported is joblib.
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:
- [Solved] Attributeerror: module tensorflow has no attribute contrib
- [Solved] Module matplotlib.cbook has no attribute iterable
- [Solved] modulenotfounderror: no module named ‘matplotlib’