No module named 'keras.wrappers.scikit_learn' (tested fix)

ModuleNotFoundError: No module named 'keras.wrappers.scikit_learn' (or 'tensorflow.keras.wrappers.scikit_learn') happens because Keras removed its scikit-learn wrappers. Install scikeras and import KerasClassifier from scikeras.wrappers; below it runs inside GridSearchCV on Keras 3.15.

1. Reproduce

try:
    from keras.wrappers.scikit_learn import KerasClassifier
except ImportError as e:
    print(type(e).__name__, "-", e)
try:
    from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
except ImportError as e:
    print(type(e).__name__, "-", e)
ModuleNotFoundError - No module named 'keras.wrappers.scikit_learn'
ModuleNotFoundError - No module named 'tensorflow.keras.wrappers.scikit_learn'

Both import paths are gone: the scikit-learn wrappers were removed from Keras and tf.keras, and Keras 3 does not have them. No Keras setting brings them back; the maintained replacement is the separate SciKeras package.

2. Fix: pip install scikeras

pip install scikeras
import keras
from scikeras.wrappers import KerasClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import GridSearchCV

keras.utils.set_random_seed(0)
X, y = make_classification(n_samples=300, n_features=8, random_state=0)

def build_model(hidden=8):
    model = keras.Sequential([
        keras.Input((8,)),
        keras.layers.Dense(hidden, activation="relu"),
        keras.layers.Dense(1, activation="sigmoid"),
    ])
    model.compile(optimizer="adam", loss="binary_crossentropy")
    return model

# old: KerasClassifier(build_fn=build_model, epochs=20, verbose=0)
clf = KerasClassifier(model=build_model, hidden=8, epochs=20, verbose=0)
grid = GridSearchCV(clf, {"hidden": [4, 16]}, cv=3)
grid.fit(X, y)
print(grid.best_params_, round(grid.best_score_, 2))
print(grid.predict(X[:5]), y[:5])
{'hidden': 16} 0.85
[1 1 1 0 0] [1 1 1 0 0]

The old build_fn= keyword still works for now, with a warning:

UserWarning - ``build_fn`` will be renamed to ``model`` in a future release, at which point use of ``build_fn`` will raise an Error instead.

During the grid search TensorFlow also printed WARNING:tensorflow:5 out of the last 9 calls to ... triggered tf.function retracing: each fold builds a new model, so this is expected and harmless here. See tf.function retracing warning for when it matters.

Related: TensorFlow Keras errors after Keras 3, Keras ImageDataGenerator deprecated.

Frequently asked questions

Why is keras.wrappers.scikit_learn missing?

The scikit-learn wrappers were removed from Keras and tf.keras; they are not part of Keras 3.

What replaces KerasClassifier from keras.wrappers?

scikeras.wrappers.KerasClassifier (pip install scikeras), which works with GridSearchCV, cross_val_score and pipelines.

Should I use build_fn or model in SciKeras?

Use model=. build_fn= still works in scikeras 0.13.0 but emits a UserWarning that it will become an error.

Written with AI assistance. Measured on 2026-10-24 with tensorflow-cpu 2.21.0, keras 3.15.1, scikeras 0.13.0, scikit-learn 1.9.1 and Python 3.13.5 on Linux. The scripts ran verbatim and the output was pasted unedited (TensorFlow startup log lines filtered). PlainTables is our extension; Keras, SciKeras and scikit-learn are free.