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-learnInstall 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 joblibChange 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 joblibwith this import statement:
from sklearn.utils import joblibDowngrade 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.1Try 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.
- cannot import name ‘safe_str_cmp’ from ‘werkzeug.security’
- Importerror failed to import any qt binding
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! 😊
Frequently Asked Questions
What is Python ImportError and what causes it?
ImportError is raised when an import fails for any reason. The most specific subtype is ModuleNotFoundError (no such module). Plain ImportError typically means the module exists but a name inside it can’t be imported, e.g. ‘cannot import name X from Y’ (X was renamed, removed, or moved between versions of Y). Common with library version mismatches.
How do I fix ‘cannot import name X from Y’?
Three steps: (1) Check the library version: pip show Y. (2) Check the changelog of Y, X may have been renamed or removed in a recent release. (3) Either pin to an older Y version (pip install Y==1.x.y) or update your code to the new import path. Common 2025-2026 examples: Werkzeug url_decode removed, Pillow ANTIALIAS renamed to LANCZOS.
Why does the import work in REPL but fail in script?
Two reasons. (1) Different Python interpreter: REPL uses one Python, your script uses another. Run python –version both times. (2) Different working directory: REPL is started where you have access to local modules, script is run from a different cwd. Add the project path to sys.path or use python -m to run as a module.
How do I avoid circular import errors?
Circular imports happen when module A imports B and B imports A at the top level. Three fixes: (1) Move one import inside the function that uses it (lazy import). (2) Restructure code so A and B both import from a third module C. (3) Use TYPE_CHECKING for type-hint-only imports: if TYPE_CHECKING: from a import X.
Where can I find more ImportError fixes?
Browse the ImportError reference hub for 67+ specific fixes (Flask, Werkzeug, Django, ML library versions). For missing-module cases see ModuleNotFoundError. For Python setup help see Python Tutorial hub.
