ImportError cannot import name 'joblib' from 'sklearn.externals'
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
This error means your code is using an old import pattern that newer scikit-learn versions no longer support. joblib used to be available through sklearn.externals, but scikit-learn removed that re-export, so modern code should import joblib directly.
The fix is usually simple: install joblib as its own package and change the import line. If the error persists after that, the remaining problem is usually environment mismatch rather than API syntax.
What Changed
Older tutorials often used:
That worked in older scikit-learn releases because joblib was exposed through sklearn.externals. Later scikit-learn versions removed that shortcut so joblib could evolve independently.
The modern import is:
This is the core answer. The package is still widely used for saving and loading scikit-learn models, but it is no longer imported through scikit-learn.
Install and Use joblib Directly
If joblib is not installed yet, install it into the same environment where your code runs.
Or with conda:
Then update the code.
That is the standard modern pattern for scikit-learn model persistence with joblib.
Check for Environment Mismatch
If you already changed the import and still get errors, verify that the Python interpreter, package installer, and execution environment are the same one.
A very common failure mode is installing joblib into one virtual environment while running the script from another.
In notebooks, also check the active kernel. Jupyter often hides environment mismatches behind what looks like a plain import problem.
For team projects, pin both scikit-learn and joblib in your dependency file so fresh environments do not drift silently.
The exact versions depend on your project, but the important practice is explicit environment management rather than assuming every machine has a compatible package set.
Be Careful When Loading Old Models
The import error itself is about the import path, not necessarily about the saved model file. But if you are loading serialized objects created under older library versions, compatibility issues can still arise separately.
That is why model persistence works best when you control:
- the library versions used to train and save the model
- the library versions used to load the model later
- the execution environment documentation around them
For production systems, pin dependencies instead of relying on whatever version is on the machine.
Common Pitfalls
A common mistake is fixing the import line in one file while an older from sklearn.externals import joblib still exists elsewhere in the project.
Another issue is installing joblib globally but running the code inside a virtual environment, container, or notebook kernel that cannot see that installation.
Developers also sometimes confuse this import error with model-compatibility problems. The import path issue is one problem; cross-version pickle or joblib loading issues are another.
Finally, do not assume that old blog posts reflect current scikit-learn practice. This specific import pattern is one of the more common examples of outdated tutorial code.
Summary
- '
joblibshould now be imported directly withimport joblib.' - '
from sklearn.externals import joblibis an old pattern that newer scikit-learn versions removed.' - Install
joblibinto the same environment where the code actually runs. - Verify the active interpreter, virtual environment, or notebook kernel if imports still fail.
- Treat import fixes and model-version compatibility as separate concerns.
Related reading
- ImportError cannot import name 'keras
- ImportError cannot import name 'to_categorical' from 'keras.utils' /usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py
- ImportError libcudnn when running a TensorFlow program
- ImportError No module named 'keras
- ImportError cannot import name 'keras_tensor' from 'tensorflow.python.keras.engine
- ImportError cannot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization
- ImportError cannot import name 'set_random_seed' from 'tensorflow' CUserspolonAnaconda3libsite-packagestensorflow__init__.py
- ImportError Could not find 'cudart64_100.dll
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.