Python
ImportError
sklearn
joblib
troubleshooting

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.

Practice ML system design

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:

python
from sklearn.externals import joblib

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:

python
import joblib

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.

bash
pip install joblib

Or with conda:

bash
conda install joblib

Then update the code.

python
1import joblib
2from sklearn.datasets import load_iris
3from sklearn.ensemble import RandomForestClassifier
4
5iris = load_iris()
6model = RandomForestClassifier(n_estimators=100, random_state=0)
7model.fit(iris.data, iris.target)
8
9joblib.dump(model, "model.joblib")
10loaded_model = joblib.load("model.joblib")
11print(loaded_model.predict(iris.data[:3]))

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.

bash
python -m pip show scikit-learn
python -m pip show joblib
python -c "import sys; print(sys.executable)"

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.

text
scikit-learn==1.5.1
joblib==1.4.2

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

  • 'joblib should now be imported directly with import joblib.'
  • 'from sklearn.externals import joblib is an old pattern that newer scikit-learn versions removed.'
  • Install joblib into 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.