scikit-learn "ImportError: cannot import name X from sklearn" in CI
The sklearn package imported, but the specific name you asked for is not defined in the installed version. scikit-learn relocates and removes estimators between releases, so a CI install of a different version breaks the import.
What this error means
A step fails with "ImportError: cannot import name 'X' from 'sklearn.Y'" with a path inside the installed sklearn package. It works locally with a pinned older or newer version.
ImportError: cannot import name 'joblib' from 'sklearn.externals'
(/opt/venv/lib/python3.12/site-packages/sklearn/externals/__init__.py)Common causes
The symbol was moved or removed in the installed release
scikit-learn removed sklearn.externals.joblib and relocated several estimators across 0.22 to 1.x. CI resolved a version where the old import path no longer exists.
An unpinned scikit-learn drifted in CI
Without a pin, CI installs the latest scikit-learn, which may have already deprecated and removed the name your code imports.
How to fix it
Import from the current location
- Read the path in the error to confirm the installed version.
- Update the import to the new module, or import the standalone package (for example
import joblib). - Re-run so the corrected import resolves.
# old: from sklearn.externals import joblib
import joblibPin scikit-learn to a known version
Pin the version that defines the symbol so CI does not silently move the import surface.
pip install "scikit-learn==1.5.2"How to prevent it
- Pin scikit-learn so its public API surface is stable in CI.
- Import
joblibdirectly rather than fromsklearn.externals. - Track deprecation warnings before they become removals.