Skip to content
Latchkey

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.

python
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

  1. Read the path in the error to confirm the installed version.
  2. Update the import to the new module, or import the standalone package (for example import joblib).
  3. Re-run so the corrected import resolves.
python
# old: from sklearn.externals import joblib
import joblib

Pin scikit-learn to a known version

Pin the version that defines the symbol so CI does not silently move the import surface.

Terminal
pip install "scikit-learn==1.5.2"

How to prevent it

  • Pin scikit-learn so its public API surface is stable in CI.
  • Import joblib directly rather than from sklearn.externals.
  • Track deprecation warnings before they become removals.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →