can't use scikit-learn - "AttributeError: 'module' object has no attribute ..."

Another cause of this problem (not the problem with the OP's code) - but the one that got me - is that python does not automatically import subpackages or modules unless that is explicitly done by the package developer. And sklearn does not automatically import its subpackages, so if you have

import sklearn 
diabetes = sklearn.datasets.load_diabetes()

then you will get

AttributeError: module 'sklearn' has no attribute 'datasets'

This is a highly misleading error message, because sklearn does have a subpackage called datasets - you just need to import it explicitly

import sklearn.datasets 
diabetes = sklearn.datasets.load_diabetes()

OK.. Found it finally.. Posting it here in case someone will get into the same problem.

I had another version of sklearn (probably because of apt-get install) in a different directory. It was partially installed somehow but it was the one that got loaded.

Make sure to look at your pip script's output to see where does it install the package, and when you load it from python, check sklearn.__path__ to see where is it taking it from.


This worked for me:

from sklearn.datasets import make_moons