ImportError: No module named sklearn.neighbors

Joseph
1 min readOct 20, 2021

Python Import Error

I ran into an “ImportError” message while running a simple K-nearest neighbors image classification. The following import code was giving me this particular error:

from sklearn.neighbors import KNeighborsClassifier

And the exact error it would immediately provide me

Traceback (most recent call last):  File "knn.py", line 5, in <module>
from sklearn.neighbors import KNeighborsClassifier
ImportError: No module named sklearn.neighbors

My first thought was to check for compatibility issues regarding Apple’s new Silicon M1 chip. Sure enough, Scikit-Learn mentions this in their install documents called “Installing on Apple Silicon M1 hardware.”

The recently introduced macos/arm64 platform (sometimes also known as macos/aarch64) requires the open source community to upgrade the build configuration and automation to properly support it.

At the time of writing (January 2021), the only way to get a working installation of scikit-learn on this hardware is to install scikit-learn and its dependencies from the conda-forge distribution, for instance using the miniforge installers:

https://github.com/conda-forge/miniforge

The following issue tracks progress on making it possible to install scikit-learn from PyPI with pip:

https://github.com/scikit-learn/scikit-learn/issues/19137

After uninstalling and reinstalling using the following code, it seems to have done the trick!

conda install -c conda-forge scikit-learn

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Joseph
Joseph

Written by Joseph

Data Scientist & Information Design Enthusiast

No responses yet

Write a response