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

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

Related

Module import issue with a Japanese Tokenizer

I am trying to get the JapaneseTokenizer working in python, but I am having trouble with one of the modules it depends on. Here is the trace of the errors I am getting:
/Users/home/PycharmProjects/SubLingo/application/tokenizerTest.py
Traceback (most recent call last):
File "/Users/home/PycharmProjects/SubLingo/application/tokenizerTest.py", line 1, in <module>
import JapaneseTokenizer
File "/Users/home/PycharmProjects/SubLingo/venv/lib/python3.7/site-packages/JapaneseTokenizer/__init__.py", line 6, in <module>
from JapaneseTokenizer.jumanpp_wrapper import JumanppWrapper
File "/Users/home/PycharmProjects/SubLingo/venv/lib/python3.7/site-packages/JapaneseTokenizer/jumanpp_wrapper/__init__.py", line 1, in <module>
from .jumanpp_wrapper import JumanppWrapper
File "/Users/home/PycharmProjects/SubLingo/venv/lib/python3.7/site-packages/JapaneseTokenizer/jumanpp_wrapper/jumanpp_wrapper.py", line 2, in <module>
from pyknp import Jumanpp
ImportError: cannot import name 'Jumanpp' from 'pyknp' (/Users/home/PycharmProjects/SubLingo/venv/lib/python3.7/site-packages/pyknp/__init__.py)
As you can see Jumanpp_wrapper is trying to import the module Jumanpp from pyknp. I have looked into the pyknp package currently installed on my machine and it does not have a module with this name. This leads me to conclude that the version of pyknp I have installed is not compatible with Jumanpp, so there must be another version available somewhere. The trouble is I install pyknp using the pip installer on my Mac, as recommended on the pyknp official site, so it should be the most current version. I'm not sure how to get an alternative version that contains the necessary module. I hope someone can point me in the right direction.
I'm a developer of the package. I kindly thank you for using my package.
I fixed bugs related to the issues here and released newer package version 1.4.
https://github.com/Kensuke-Mitsuzawa/JapaneseTokenizers/releases/tag/1.4
You could install/upgrade the package with pip also.
Install pip install JapaneseTokenizer / Upgrade pip install -U JapaneseTokenizer
I have been in direct contact with the developer of JapaneseTokenizer who has kindly given permission for me to repost his answer to my query:
I'm glad that you sent me a message about the issue. I read your post at StackOverflow. As other user suggested, the main issue is that pyknp package does not have juman++ module. I don't know the reason, but an author of pyknp package removed module for juman++.
The straightforward way to solve this issue is that you install pyknp package version 3 from here and install it your environment.
The main procedure is below.
remove pyknp package from your environment pip uninstall pyknp
get download pyknp package previous version. http://nlp.ist.i.kyoto-u.ac.jp/DLcounter/lime.cgi?down=http://lotus.kuee.kyoto-u.ac.jp/nl-resource/pyknp/pyknp-0.3.tar.gz&name=pyknp-0.3.tar.gz
install the pyknp=0.3 with pip install pyknp-0.3.tar.gz
From now, I revise JapaneseTokenizer package. It might take some weeks. Next time you try to install JapaneseTokenizer package, everything will be fine.
Again, thanks for giving me a message.
Best,
Kensuke Mitsuzawa

ImportError: cannot import name ExponentialSmoothing

I tried to install statsmodels in python. After installation, I checked with pip freeze. The package can be seen in the list.
When I am trying:
from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt
I am getting error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name ExponentialSmoothing
I have tried the following link also :
link
As of today (10 May 2018), the problem is solved by simply installing version 0.9.0 rather than the default 0.8.0:
pip install statsmodels==0.9.0rc1
I met the same situation, and the install process recommended in Nish's url didn't work for me. Here's how did I solve the problem (I'm using Mac OS).
Remove statsmodels library first, if you have installed: pip uninstall statsmodels
In your terminal, type git init, to initiate git
Then type git clone git://github.com/statsmodels/statsmodels.git
Change the directory to statsmodels using “cd statsmodels”
Next type python setup.py install
python setup.py build_ext --inplace
Now type python in your terminal and then type from statsmodels.tsa.api import ExponentialSmoothing, to see whether it can import successfully
If using conda, this will make statsmodel 0.9.0
conda update statsmodels
It is the wrong import,
Try
from statsmodels.tsa.holtwinters import ExponentialSmoothing, SimpleExpSmoothing, Holt
You can follow the steps mentioned below:
Step 1: Remove statsmodel using pip uninstall statsmodel
Step 2: Install git from here: https://git-scm.com/downloads
Step 3: Follow steps mentioned under "Installing library(statsmodels)" from link mentioned below:
https://www.analyticsvidhya.com/blog/2018/02/time-series-forecasting-methods/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+AnalyticsVidhya+%28Analytics+Vidhya%29

ValueError: numpy.dtype has the wrong size, try recompiling

I just installed pandas and statsmodels package on my python 2.7
When I tried "import pandas as pd", this error message comes out.
Can anyone help? Thanks!!!
numpy.dtype has the wrong size, try recompiling
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\formula\__init__.py",
line 4, in <module>
from formulatools import handle_formula_data
File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\formula\formulatools.p
y", line 1, in <module>
import statsmodels.tools.data as data_util
File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\tools\__init__.py", li
ne 1, in <module>
from tools import add_constant, categorical
File "C:\analytics\ext\python27\lib\site-packages\statsmodels-0.5.0-py2.7-win32.egg\statsmodels\tools\tools.py", line
14, in <module>
from pandas import DataFrame
File "C:\analytics\ext\python27\lib\site-packages\pandas\__init__.py", line 6, in <module>
from . import hashtable, tslib, lib
File "numpy.pxd", line 157, in init pandas.tslib (pandas\tslib.c:49133)
ValueError: numpy.dtype has the wrong size, try recompiling
(to expand a bit on my comment)
Numpy developers follow in general a policy of keeping a backward compatible binary interface (ABI). However, the ABI is not forward compatible.
What that means:
A package, that uses numpy in a compiled extension, is compiled against a specific version of numpy. Future version of numpy will be compatible with the compiled extension of the package (for exception see below).
Distributers of those other packages do not need to recompile their package against a newer versions of numpy and users do not need to update these other packages, when users update to a newer version of numpy.
However, this does not go in the other direction. If a package is compiled against a specific numpy version, say 1.7, then there is no guarantee that the binaries of that package will work with older numpy versions, say 1.6, and very often or most of the time they will not.
The binary distribution of packages like pandas and statsmodels, that are compiled against a recent version of numpy, will not work when an older version of numpy is installed.
Some packages, for example matplotlib, if I remember correctly, compile their extensions against the oldest numpy version that they support. In this case, users with the same old or any more recent version of numpy can use those binaries.
The error message in the question is a typical result of binary incompatibilities.
The solution is to get a binary compatible version, either by updating numpy to at least the version against which pandas or statsmodels were compiled, or to recompile pandas and statsmodels against the older version of numpy that is already installed.
Breaking the ABI backward compatibility:
Sometimes improvements or refactorings in numpy break ABI backward compatibility. This happened (unintentionally) with numpy 1.4.0.
As a consequence, users that updated numpy to 1.4.0, had binary incompatibilities with all other compiled packages, that were compiled against a previous version of numpy. This requires that all packages with binary extensions that use numpy have to be recompiled to work with the ABI incompatible version.
For me (Mac OS X Maverics, Python 2.7)
easy_install --upgrade numpy
helped. After this you can install up-to-date packages pandas, scikit-learn, e.t.c. using pip:
pip install pandas
I found it to be a simple version being outdated or mismatch and was fixed with:
pip install --upgrade numpy
pip install --upgrade scipy
pip install --upgrade pandas
Or might work with the one liner:
pip install --upgrade numpy scipy pandas
I had a similar error with another library and realized that I had several versions of numpy installed on my system. The fix for me was to edit my PYTHONPATH and put the site-packages that contained the latest version of numpy in first position.
As in here, for me only sudo pip install pandas==0.13.1 worked
I also encounter this error when use pandas to access MYSQL.
This error message indicates a binary compatible issue and can be resolved by
using latest version of pandas and numpy package.
Here is my steps to resolve this issue, and it works well on my Ubuntu 12.04:
cd /tmp/
wget https://pypi.python.org/packages/source/p/pandas/pandas-0.12.0.tar.gz
tar xzvf pandas-0.12.0.tar.gz
cd pandas-0.12.0
easy_install --upgrade numpy
In my case, I had installed pandas-0.10.0.win-amd64-py2.7 but was checking to see if a bug had been fixed in a more recent version of pandas. So I did an easy_install -U to force the upgrade, but then got the above error due to some incompatibilities with numpy etc... when I did
import pandas
To fix, I just reinstalled the pandas-0.10.0.win-amd64-py2.7 binary and everything works. I didn't see this answer (suggests to use pip) which may have helped me (though not sure) Install particular version with easy_install
Also this highlights why one should use virtualenv (which I wasn't).
For me (Mac OS X Mavericks) it worked to install the version for python2.6:
sudo port install py26-scikit-learn
then run:
python2.6 myscript.py
The problem I solved on Webfaction was old numpy library(1.5) which was in conflict with my fresh
pip install pandas
installation in .virtualenv.
The problem was solved after I did pip install pandas out of the virtual environment.
The idea came from discussion on https://github.com/pydata/pandas/issues/3711, thanks, cpcloud!
I just meet this 'ValueError' issue and have addressed it. Definitely there's something wrong with numpy package.
But when I try to pip install --upgrade numpy it failed, so I uninstall and download the newest numpy.zip file.
Then manually uncompress and python setup.py install it.
Luckly, it works!
Like #user333700 said, required versions of libraries may not meet for each other. You get one library as another's dependency. Then without knowing it was already installed as dependency, you need that specific library and you install one version. With such ways dependencies may mess up.
I lived such a case and looked for a solution. Found this:
https://stackoverflow.com/a/12975518/1694344
I had two different versions for egg-info file and folder name of numpy:
drwxr-xr-x. 19 root root 4096 Sep 25 15:00 numpy
drwxr-xr-x. 2 root root 4096 Sep 22 11:25 numpy-1.13.1.dist-info
-rw-r--r--. 1 root root 1630 Nov 20 2015 numpy-1.7.1-py2.7.egg-info
I removed them all and reinstalled numpy with pip.
I had a similar issue, and simply re-installing using pip install ... as suggested in previous comments didn't work.
What worked for me was re-installing with the added flag pip install --no-cache-dir ..., seems there was an incompatible numpy version somewhere in the cache.
There are cases where you want to keep a specific NumPy version and the upgrade option mentioned here will not work.
An example that occurred to me was the Python distribution preinstalled with ArcGIS. For ArcPy to work in ArcGIS 10.5.1, that distribution needs to be Python 2.7.12 with NumPy 1.9.3 and any other version of NumPy is probably going to cause issues with your ArcPy functionality.
What you can do with this case is try to install a specific, older version of the problematic third-party library that is supposed to be compatible with the older NumPy version that ArcGIS has.
For instance, scikit-learn 0.19.1 would NOT operate with NumPy 1.9.3 and would result in the same error you mentioned. However, scikit-learn 0.15 works fine. You can test different versions to find the one that works. Just mention the version number through pip:
python -m pip install scikit-learn==0.15

problems installing M2Crypto on Mint: follow-up

I asked a question here regarding installing the M2Crypto python library in Mint. That question was answered successfully and I was able to build and install M2Crypto. However, I am not able to use it within Python. when I attempt to import the module, I get the following error:
>>> import M2Crypto
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/M2Crypto/__init__.py", line 22, in <module>
import __m2crypto
ImportError: /usr/local/lib/python2.7/dist-packages/M2Crypto/__m2crypto.so: undefined symbol: SSLv2_method
If anyone know how to resolve this error, please let me know.
After two years the problem in m2crypto0.21.1 still exists, so here is a note for Ubuntu users. If you're not using virtualenv, you can easily fix it by installing m2crypto from Ubuntu PPA instead of pip:
sudo pip uninstall m2crypto
sudo apt-get install python-m2crypto
Seems like the problem is fixed in the PPA, but not in the project's repo. At least it worked for me at Ubuntu 12.04.
To expand the answer above:
This is a bug in M2Crypto 0.21.1. It has to do with the fact that SSLv2 was removed from OpenSSL on many platforms on the grounds of it being too insecure, notably Debian [1] and Ubuntu.
The removal is not detected during compile. There is a widely available patch that fixes this. I incorporated this into the M2Crypto source code on Github.
You can use pip to install directly from the repository like so:
pip install -e git+https://github.com/Hypernode/m2crypto#egg=M2Crypto
For those who want to inspect the (much published) patch that fixes this: b432d36
Edit: moved to another location
It looks like it is a bug introduced in M2Crypto 0.21.1, there is a patch here that correct the problem.
Maybe you could try to install an old version of M2Crypto (like the 0.20.1-1.1) or apply the patch manually with the patch command.

Error when running tests of scipy and numpy on OS X Snow Leopard

I am new to stackoverflow as well as Python, and I hope to use stackoverflow to learn and improve my Python programming.
However, as soon as I set up Python, SciPy, NumPy on my Mac, I encountered a problem when I tried running a full test of SciPy and NumPy to verify the install:
>>> import scipy
>>> scipy.test()
Running unit tests for scipy
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
scipy.test()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/nosetester.py", line 318, in test
self._show_system_info()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/nosetester.py", line 187, in _show_system_info
nose = import_nose()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/nosetester.py", line 69, in import_nose
raise ImportError(msg)
ImportError: Need nose >= 0.10.0 for tests - see http://somethingaboutorange.com/mrl/projects/nose
As suggested by the error message, I install this 'nose' package via easy_install, and the install seems successful as I get the following from bash:
mlstr:~ mlstr$ nosetests -V
nosetests version 1.1.2
So I tried the SciPy test again, and it failed with the same message. Can someone please let me know if I need to do anything else to install/configure nose so that it can be used by the unit tests of SciPy and NumPy. Thanks in advance.
UPDATE #1: After trying Rahul's suggestion of using pip to reinstall nose, I think I've found where the problem is: whenever I do easy_install or pip from bash, the package is installed into Python 2.6 directories instead of 2.7 ones. And I think this is because by default 2.6 is the one shipped with Snow Leopard. So what should I do to tell easy_install or pip that I want to install a particular package for Python 2.7? Thanks.
UPDATE #2: By following up my update above, I've found the solution here: easy_install with various versions of python installed, mac osx. It is indeed because that I used the wrong version of easy_install thus my nose installation was for Python 2.6 shipped with Mac OS X. Thanks.
I tried replicating the error that you were encountering. I did not have nose, so I was getting the same error.
I installed nose using pip
sudo pip install nose
After that the scipy.test() worked. I did nothing else. Did you install nose using pip? If not, try using pip.
My machine has :
SciPy version 0.10.0.dev
Nose - 1.1.2
If you want to use your locally installed python version you need to make sure, that this is called when you run python from console.
On a linux machine an alias would do that. Once you run the correct python from your console all the easy_install things will be available in the correct python version.

Categories