Unable to solve this error: ImportError [duplicate] - python

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.

Worked for me after installing scipy.

>>> 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!

My solution for Python 3.6.5 64-bit Windows 10:
pip uninstall sklearn
pip uninstall scikit-learn
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.

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;

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.

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.

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
>>>

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

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.

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

no need to uninstall & then re-install sklearn
try this:
from sklearn.model_selection import train_test_split

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

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

Related

Error when attempting to import sklearn in Jupyter Notebook [duplicate]

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.
Worked for me after installing scipy.
>>> 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!
My solution for Python 3.6.5 64-bit Windows 10:
pip uninstall sklearn
pip uninstall scikit-learn
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.
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;
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.
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.
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
>>>
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
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.
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
no need to uninstall & then re-install sklearn
try this:
from sklearn.model_selection import train_test_split
i had the same problem reinstalling anaconda solved the issue for me
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

Scipy Import Error; cannot import name NUMPY_MKL

I used python xy recently with an old lib of scipy (0.15). Because I wanted to use Spherical Voronoi, I had to install a more recent version of scipy. For this I used pip and the unofficial distributions for python modules with wheel. The install worked without error messages. But now, if I try to import scipy by code, I get this error message below. I already unistalled whole python packages so far and instead installed anaconda. But the error message is still the same. I checked my registry (Windows 10) - can't find anything suspicious.
Any idea? Thank you!
test.py
1.12.1
Traceback (most recent call last):
File "test.py", line 11, in <module>
import scipy
File "C:\Users\Boss\AppData\Roaming\Python\Python27\site-packages\scipy\__init__.py", line 61, in <module>
from numpy._distributor_init import NUMPY_MKL # requires numpy+mkl
ImportError: cannot import name NUMPY_MKL
Your scipy is built with mkl support and requires mkl support in numpy too. Download numpy from the same site you downloaded scipy (probably Christoph Gohlke's builds). Or if you are using Anaconda use Anaconda packages for both.
Updated - Your error is basically that  numpy+mkl (numpy with Intel math Kernel library) so this is because when you have installed the scipy by precompiled archive, which requires  numpy+mkl but installing numpy using pip won't get you that.
This problem can be easy solved by installation for numpy+mkl from whl file from - http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy
Old - This is due to the improper installation of numpy, I've had this error few days ago and then I had to install the numpy again. So, upgrade pip and then try installing the numpy whl again. It just worked for me.
Get it here -https://pypi.python.org/pypi/numpy
even this has almost everything - http://www.lfd.uci.edu/~gohlke/pythonlibs/

How to import Scipy and Numpy in Python?

I am very new to Python and want to add Numpy and Scipy module. I think my question is very simple for you. I am using Python 3.06a.1 version. I think I already installed something called Anaconda that contains those library. When I type import scipy I get the following message:
import scipy
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
import scipy
ImportError: No module named 'scipy'
also when I want to installed with command line I get the following message which means that I have it already.
localhost:~ user$ pip install scipy
Requirement already satisfied (use --upgrade to upgrade): scipy in ./anaconda/lib/python3.5/site-packages
localhost:~ user$
Please help me to fix this problem
You shouldn't have problem with scipy and numpy if you installed Anaconda. What I'm advising you may sound stupid, but I'm sure it has a good chance to solve your problem.
Relaunch Anaconda, reboot your computer, reinstall Anaconda.
Edit : also watch out to use "scipy" and not "spicy" as I just witnessed in your logs.
if you are using anaconda, try installing scipy using anaconda:
conda install scipy

NameError: global name 'dot_parser' is not defined

I was playing with the decision tree algorithm and trying to plot the tree. However the IDE reported following error:
Couldn't import dot_parser, loading of dot files will not be possible.
<class 'pandas.core.frame.DataFrame'>
Traceback (most recent call last):
File "C:/Users/s152730/Desktop/exe1.py", line 70, in <module>
graph = pydot.graph_from_dot_data(test.getvalue())
File "C:\Python27\lib\site-packages\pydot.py", line 220, in graph_from_dot_data
return dot_parser.parse_dot_data(data)
NameError: global name 'dot_parser' is not defined
I have no idea how to deal with this problem because I've tried to uninstall and reinstall both pydot dan pyparsing, which was proposed in other answers, but it didn't help.
Here is my code:
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import ExtraTreeClassifier
from sklearn import tree
from sklearn.externals.six import StringIO
import pydot
from IPython.display import Image
test = StringIO()
tree.export_graphviz(clf, out_file=test, feature_names = attribute_names)
graph = pydot.graph_from_dot_data(test.getvalue())
graph.writepng('test.png')
image(filename = 'test.png')
I am using python2.7 and running on PyCharm, the OS is win8.1.
Thanks for your help.
It seems your error is that you are missing part of a library (pyparsing) because of incorrect order of installation.
See here and here
Obvious to the initiated but not to the newbie: The workaround is to
install pyparsing < 2.0.0 prior to installing pydot (or a package
that depends on pydot.)
$ pip install pyparsing==1.5.7
The solution seems to be to first remove pydot and pyparsing, and then to install pyparsing first, then pydot.
The versions of which to install will most likely change in the future, so at the moment it seems you need to run something like the following: (taken from this lovely answer)
pip uninstall pyparsing
pip install -Iv https://pypi.python.org/packages/source/p/pyparsing/pyparsing-1.5.7.tar.gz#md5=9be0fcdcc595199c646ab317c1d9a709
pip install pydot
For me, I found a great tip is to install pydotplus instead, as it is compatible with pyparsing v2.0 and greater. It also has the advantage that it can work with the graphviz installation from Anaconda. I'm using Anaconda v2.4.1 and on Windows 7 x64 and Graphviz 2.38 installed using condas.
I've just update my pydotto 1.2.3, and the error disappears.
sudo pip install -U pydot

Python: Why can't I import svm? [duplicate]

Using Python 2.7 with scikit-learn 0.14 package. It runs well on some examples from the user guild expect the Linear Models.
Traceback (most recent call last):
File "E:\P\plot_ols.py", line 28, in <module>
from sklearn import datasets, linear_model
File "C:\Python27\lib\site-packages\sklearn\linear_model\__init__.py", line 12, in <module>
from .base import LinearRegression
File "C:\Python27\lib\site-packages\sklearn\linear_model\base.py", line 29, in <module>
from ..utils.sparsefuncs import mean_variance_axis0, inplace_column_scale
ImportError: cannot import name inplace_column_scale
Thank you~
I was able to fix this by going to my python folder and deleting the file:
python27\Lib\site-packages\sklearn\utils\sparsefuncs.pyd
My guess is that the problem was:
An older version of scikit-learn implemented sparsefuncs as a windows DLL
The current version implements it as a python file
If you install a new version on top of an old version it does not delete the old DLL
When you try to import, Python uses the pyd in preference to the py implementation
But the old implementation did not include this function
This suggests that there might be bigger problems caused by installing a new version and it might be wise to delete the whole sklearn directory before reinstalling the new version.
I encountered the same issue in Mac Os.
I solved it by deleting the file manually:
rm /usr/local/lib/python2.7/site-packages/sklearn/utils/sparsefuncs.so
Uninstalling scikit-learn and reinstalling it was the only option that worked for me:
pip uninstall scikit-learn
pip install scikit-learn
I solve this problem by :
pip uninstall scikit-learn
and don't forget to rm the 'sklearn' folder in the python 'site-packages'
rm -rf /path/Python-2.7.5/lib/python2.7/site-packages/sklearn/
then reinstall the package:
pip install scikit-learn
On a related note, this has been posted as a bug on the official Github page, along with some additional solutions, basically suggesting the same solutions as above. Long story short: run a make clean to get rid of the .so file.
I had the same problem. I had originally installed scikit-learn by:
sudo apt-get install python-sklearn
When none of the other solutions posted here worked, I decided to uninstall my version of scikit-learn and reinstall it:
pip2 install --user --install-option="--prefix=" -U scikit-learn
Used pip2 because I have two versions of python, so I use scikit-learn in Python 2.7
same problem happened with can not import _safe_split, have a look
http://stackoverflow.com/questions/41024001/importerror-cannot-import-name-safe-split
https://github.com/scikit-learn/scikit-learn/issues/7582
EDIT: check comment by Andreas Mueller

Categories