132

I am getting the following error while trying to import from sklearn:

>>> from sklearn import svm

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
   from sklearn import svm
  File "C:\Python27\lib\site-packages\sklearn\__init__.py", line 16, in <module>
   from . import check_build
ImportError: cannot import name check_build

I am using python 2.7, scipy-0.12.0b1 superpack, numpy-1.6.0 superpack, scikit-learn-0.11 I have a windows 7 machine

I have checked several answers for this issue but none of them gives a way out of this error.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
ayush singhal
  • 1,879
  • 2
  • 18
  • 33

13 Answers13

162

Worked for me after installing scipy.

Sawan
  • 2,152
  • 1
  • 14
  • 14
  • 30
    Plus restarting the python shell. :) – mannuscript Jan 07 '18 at 09:36
  • 1
    Check if there is a file called "sklearn" in the same folder. Try running from a different folder. Solved it for me (my fault). Find file with – Punnerud Mar 03 '18 at 11:50
  • @Mannu Yes; Also for me on jupyter notebook, just restarting (shutting down and click-open again) that ipynb py-kernel worked without restarting all of the jupyter notebook. – Abhimanu Kumar May 02 '18 at 15:01
  • `scipy.stats` also did not work also raised an error. In my case, I had a file called `multiprocessing.py` in the same directory that somehow conflicted with the kernel's socket connection. Fixed by renaming the file `multiprocessing_.py` (leading underscore did not work). – pylang Jan 11 '19 at 02:52
51
>>> from sklearn import preprocessing, metrics, cross_validation

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    from sklearn import preprocessing, metrics, cross_validation
  File "D:\Python27\lib\site-packages\sklearn\__init__.py", line 31, in <module>
    from . import __check_build
ImportError: cannot import name __check_build
>>> ================================ RESTART ================================
>>> from sklearn import preprocessing, metrics, cross_validation
>>> 

So, simply try to restart the shell!

Gurupad Hegde
  • 2,155
  • 15
  • 30
  • 20
    Magic answers like this are not really helpful because they don't solve the problem. What if a restart doesn't fix it? – icedwater Sep 23 '14 at 08:41
  • 4
    Just mentioning that this happened to me with scikit-learn 0.15.2 compiled against numpy 1.9 and python 2.7. – alexbw Oct 09 '14 at 14:13
  • 3
    If you don't want to restart, `del sys.modules['sklearn.__check_build']; import sklearn` will also work – Jared Forsyth Mar 23 '15 at 22:55
23

My solution for Python 3.6.5 64-bit Windows 10:

  1. pip uninstall sklearn
  2. pip uninstall scikit-learn
  3. pip install sklearn

No need to restart command-line but you can do this if you want. It took me one day to fix this bug. Hope this help.

Loitd
  • 331
  • 2
  • 3
  • 1
    If you're essentially saying that the latest version of sklearn fixes the bug, it would be helpful to state which version numbers of sklearn you upgrade to. Thanks. – rwp Jun 06 '18 at 04:37
  • In Windows 10 I also had to enable long paths before executing these einstructions https://stackoverflow.com/questions/54778630/could-not-install-packages-due-to-an-environmenterror-errno-2-no-such-file-or – Napuu Sep 09 '20 at 13:14
9

After installing numpy , scipy ,sklearn still has error

Solution:

Setting Up System Path Variable for Python & the PYTHONPATH Environment Variable

System Variables: add C:\Python34 into path User Variables: add new: (name)PYTHONPATH (value)C:\Python34\Lib\site-packages;

Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
user3236650
  • 101
  • 1
  • 1
7

Usually when I get these kinds of errors, opening the __init__.py file and poking around helps. Go to the directory C:\Python27\lib\site-packages\sklearn and ensure that there's a sub-directory called __check_build as a first step. On my machine (with a working sklearn installation, Mac OSX, Python 2.7.3) I have __init__.py, setup.py, their associated .pyc files, and a binary _check_build.so.

Poking around the __init__.py in that directory, the next step I'd take is to go to sklearn/__init__.py and comment out the import statement---the check_build stuff just checks that things were compiled correctly, it doesn't appear to do anything but call a precompiled binary. This is, of course, at your own risk, and (to be sure) a work around. If your build failed you'll likely soon run into other, bigger problems.

BenDundee
  • 4,389
  • 3
  • 28
  • 34
  • I do not have _check_build.so in the folder , What can I do to get this for python 2.7 – ayush singhal Mar 07 '13 at 16:09
  • Did you try commenting out the line that tries to import it in `sklearn/__init__.py`? – BenDundee Mar 07 '13 at 18:18
  • 2
    For the moment, you can comment out the line that does the import, that should get you running. Can you then please open an issue in the issue tracker https://github.com/scikit-learn/scikit-learn/issues looks like a bug. – Andreas Mueller Mar 09 '13 at 11:35
  • 2
    FWIW Commenting out the `import` in `__init__.py` didn't fix the problem but did expose a different error - my scipy install had not succeeded. The error in the question was masking the real issue – Basic Sep 01 '14 at 12:26
4

I had the same issue on Windows. Solved it by installing Numpy+MKL from http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy (there it's recommended to install numpy+mkl before other packages that depend on it) as suggested by this answer.

Community
  • 1
  • 1
user2314737
  • 27,088
  • 20
  • 102
  • 114
4

I had problems importing SKLEARN after installing a new 64bit version of Python 3.4 from python.org.

Turns out that it was the SCIPY module that was broken, and alos failed when I tried to "import scipy".

Solution was to uninstall scipy and reinstall it with pip3:

C:\> pip uninstall scipy

[lots of reporting messages deleted]

Proceed (y/n)? y
  Successfully uninstalled scipy-1.0.0

C:\Users\>pip3 install scipy

Collecting scipy
  Downloading scipy-1.0.0-cp36-none-win_amd64.whl (30.8MB)
    100% |████████████████████████████████| 30.8MB 33kB/s
Requirement already satisfied: numpy>=1.8.2 in c:\users\johnmccurdy\appdata\loca
l\programs\python\python36\lib\site-packages (from scipy)
Installing collected packages: scipy
Successfully installed scipy-1.0.0

C:\Users>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
>>>
>>> import sklearn
>>>
3

If you use Anaconda 2.7 64 bit, try

conda upgrade scikit-learn

and restart the python shell, that works for me.

Second edit when I faced the same problem and solved it:

conda upgrade scikit-learn

also works for me

Statham
  • 4,000
  • 2
  • 32
  • 45
2

None of the other answers worked for me. After some tinkering I unsinstalled sklearn:

pip uninstall sklearn

Then I removed sklearn folder from here: (adjust the path to your system and python version)

C:\Users\%USERNAME%\AppData\Roaming\Python\Python36\site-packages

And the installed it from wheel from this site: link

The error was there probably because of a version conflict with sklearn installed somewhere else.

0x1996
  • 69
  • 5
chross
  • 61
  • 1
2

For me, I was upgrading the existing code into new setup by installing Anaconda from fresh with latest python version(3.7) For this,

from sklearn import cross_validation, 
from sklearn.grid_search import GridSearchCV

to

from sklearn.model_selection import GridSearchCV,cross_validate
Siddaram H
  • 1,126
  • 12
  • 17
2

no need to uninstall & then re-install sklearn

try this:

from sklearn.model_selection import train_test_split
Cathrine
  • 131
  • 10
0

In windows:

I tried to delete sklearn from the shell: pip uninstall sklearn, and re install it but doesn't work ..

the solution:

1- open the cmd shell.
2- cd c:\pythonVERSION\scripts
3- pip uninstall sklearn
4- open in the explorer: C:\pythonVERSION\Lib\site-packages
5- look for the folders that contains sklearn and delete them ..
6- back to cmd: pip install sklearn
Minions
  • 5,104
  • 5
  • 50
  • 91
0

i had the same problem reinstalling anaconda solved the issue for me