Scikit-Learn: ModuleNotFoundError: No module named model_selection / sklearn.model_selection
This error occurs when Python tries to import model_selection directly without the sklearn prefix, or when using an outdated Scikit-Learn release (< 0.18). Upgrade scikit-learn via pip install --upgrade scikit-learn and use from sklearn.model_selection import ...
Root Cause Analysis
This error occurs when Python code attempts to execute import model_selection (missing the top-level sklearn. namespace) or when running on an obsolete installation of Scikit-Learn where model_selection was not yet introduced.
Root Cause 1: Missing Top-Level Package Name in Import Statement
model_selection is a submodule of sklearn, not a standalone top-level package on PyPI. Writing import model_selection instead of from sklearn.model_selection import train_test_split raises ModuleNotFoundError: No module named 'model_selection'.
Root Cause 2: Legacy Scikit-Learn Version (< 0.18)
In ancient versions of Scikit-Learn (prior to 0.18 released in 2016), model_selection did not exist. Legacy virtual environments or embedded Python runtimes containing older versions fail when encountering modern code.
Root Cause 3: Broken Virtual Environment Package Mapping
If multiple conflicting Scikit-Learn installations exist across system Python and virtual environments, Python may load an outdated installation directory.
Root Cause 4: Local Module Shadowing (sklearn.py)
A local file named sklearn.py in the workspace prevents Python from accessing the genuine sklearn.model_selection submodule.
Reproduction Code (MCVE)
import sys
# Simulating missing top-level module resolution when omitting sklearn prefix
def try_standalone_model_selection_import():
# model_selection cannot be imported as a top-level package
raise ModuleNotFoundError(
"ModuleNotFoundError: No module named 'model_selection'. "
"Did you mean: 'from sklearn.model_selection import train_test_split'?"
)
try_standalone_model_selection_import()
Solution 1: Use the Full from sklearn.model_selection Import Path
Always import from the sklearn.model_selection package namespace.
# Solution 1: Correct import syntax
from sklearn.model_selection import train_test_split, cross_val_score
import numpy as np
# Verify successful import and execution
data = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
labels = np.array([0, 1, 0, 1])
train_x, test_x, train_y, test_y = train_test_split(data, labels, test_size=0.5, random_state=42)
print("Data split successfully. Train samples:", len(train_x))
assert len(train_x) == 2
Solution 2: Upgrade Scikit-Learn to the Latest Stable Release
Upgrade your environment to modern Scikit-Learn using pip.
# Solution 2: Upgrade command
cmd = "pip install --upgrade scikit-learn"
print("Execute in terminal:")
print(f" $ {cmd}")
assert "upgrade scikit-learn" in cmd
Never run pip install model_selection or pip install sklearn.model_selection. The package to install on PyPI is scikit-learn (pip install scikit-learn).
Note de reproductibilité : Cette erreur dépend de la syntaxe de l'import et de la version de scikit-learn installée dans l'environnement virtuel.
Contrast import model_selection with from sklearn import model_selection: from sklearn import model_selection correctly imports the submodule from the parent package.