shaare.it

ModuleNotFoundError: No module named 'sklearn'

ModuleNotFoundError: No module named ‘sklearn’

$ python -c "import sklearn"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'sklearn'

Why this happens

  • scikit-learn is not installed in the active environment.
  • You’re using a different Python interpreter than the one with scikit-learn.

Fix

  • Install scikit-learn in the current environment and verify which Python is used.

Wrong code

import sklearn
# Attempt to use sklearn without installation

Fixed code

# After installing: pip install -U scikit-learn
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
print(len(X), len(y))