121

Code:

import numpy as np
import cv

Console:

>>> runfile('/Users/isaiahnields/.spyder2/temp.py', wdir='/Users/isaiahnields/.spyder2')
RuntimeError: module compiled against API version a but this version of numpy is 9
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/Users/isaiahnields/.spyder2/temp.py", line 9, in <module>
import cv
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/cv.py", line 1, in <module>
from cv2.cv import *
ImportError: numpy.core.multiarray failed to import
>>> 

System Info: OS X El Capitan, Macbook Air, 1.3 GHz Intel Core i5, 8 GB 1600 HMz DDR3

I have already attempted updating numpy. I had to add cv.py to the python2.7 folder in Spyder-Py2 is there something else I need to add?

Isaiah Nields
  • 1,221
  • 2
  • 8
  • 4

21 Answers21

162

Upgrade numpy to the latest version

pip install numpy --upgrade
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Joshua Owoyemi
  • 1,827
  • 1
  • 10
  • 7
15

Check the path

import numpy
print numpy.__path__

For me this was /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy So I moved it to a temporary place

sudo mv /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy \
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy_old

and then the next time I imported numpy the path was /Library/Python/2.7/site-packages/numpy/init.pyc and all was well.

JnBrymn
  • 24,245
  • 28
  • 105
  • 147
15

This worked for me:

sudo pip install numpy --upgrade --ignore-installed
a20
  • 5,495
  • 2
  • 30
  • 27
  • 3
    This worked for me as well. I had the same problem where an old numpy version was installed and i was unable to move or remove old numpy – Amit Singh Mar 12 '18 at 11:39
  • 3
    Don't do sudo pip install ! – ComputerScientist Jul 12 '21 at 00:49
  • This worked for me, thanks! Although I did not use it with sudo, I don't think that is required. – Forgotten-Storm Mar 01 '23 at 18:52
  • I received an edit request to remove "sudo" and add some other parameters. I understand the editor felt that that would be a better answer - however I rejected that edit, because *this* is what worked for *me*, and that's all I'm sharing. If you have any other answer, please feel free to post your own answer. – a20 Mar 10 '23 at 03:51
8

upgrading numpy to rescue

numpy official document suggests users to do upgrade to solve this issue [1].

pip install numpy --upgrade

but which version of numpy should I upgrade to?

You may upgrade to a version that is too new/old for your environment. I spent a long time trying to figure out which version of numpy is expected to be upgraded to when running into this error, and here is a list [2] of numpy versions with their corresponding C API versions, which may be useful for troubleshooting such an issue:

# 0x00000008 - 1.7.x
# 0x00000009 - 1.8.x
# 0x00000009 - 1.9.x
# 0x0000000a - 1.10.x
# 0x0000000a - 1.11.x
# 0x0000000a - 1.12.x
# 0x0000000b - 1.13.x
# 0x0000000c - 1.14.x
# 0x0000000c - 1.15.x
# 0x0000000d - 1.16.x
# 0x0000000d - 1.19.x
# 0x0000000e - 1.20.x
# 0x0000000e - 1.21.x
# 0x0000000f - 1.22.x
# 0x00000010 - 1.23.x
# 0x00000010 - 1.24.x
  • You can find the list here [2].

  • And the C API VERSION in numpy is tracked in three places according to [3]:

    • numpy/core/setup_common.py
    • numpy/core/code_generators/cversions.txt
    • numpy/core/include/numpy/numpyconfig.h
  • The error is reported by numpy's source code here [4]

references

nybon
  • 8,894
  • 9
  • 59
  • 67
4

You are likely running the Mac default (/usr/bin/python) which has an older version of numpy installed in the system folders. The easiest way to get python working with opencv is to use brew to install both python and opencv into /usr/local and run the /usr/local/bin/python.

brew install python
brew tap homebrew/science
brew install opencv
Pat Niemeyer
  • 5,930
  • 1
  • 31
  • 35
  • 2
    Becareful! It might not be a good idea for you to install multiple versions of python. You may end up like this guy: http://stackoverflow.com/questions/14117945/too-many-different-python-versions-on-my-system-and-causing-problems – Jakobovski May 04 '16 at 10:39
4

I ran into the same issue tonight. It turned out to be a problem where I had multiple numpy packages installed. An older version was installed in /usr/lib/python2.7 and the correct version was installed in /usr/local/lib/python2.7.

Additionally, I had PYTHONPATH=/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages. PYTHONPATH was finding the older version of numpy before the correct version, so when inside the Python interpreter, it would import the older version of numpy.

One thing which helped was opening a python session an executing the following code:

import numpy as np 
print np.__version__ 
print np.__path__

That should tell you exactly which version Python is using, and where it's installed.

To fix the issue, I changed PYTHONPATH=/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages. And I also setup a virtual Python environment using the Hitchiker's Guide to Python, specifically the section titled "Lower level: virtualenv" . I know I should have setup a virtual environment in the first place, but I was tired and being lazy. Oh well, lesson learned!

(Update)

Just in case the docs are moved again, here are the relevant bits on...

Creating a Python Virtual Environment

Install virtualenv via pip:

$ install virtualenv

Test the installation:

$ virtualenv --version

Optionally, et the environment variable VIRTUALENVWRAPPER_PYTHON to change the default version of python used by virtual environments, for example to use Python 3:

$ export VIRTUALENVWRAPPER_PYTHON=$(which python3)

Optionally, set the environment variable WORKON_HOME to change the default directory your Python virtual environments are created in, for example to use /opt/python_envs:

$ export WORKON_HOME=/opt/python_envs

Create a virtual environment for a project:

$ cd my_project_folder
$ virtualenv my_virtual_env_name

Activate the virtual environment, you just created. Assuming you also set WORKON_HOME=/opt/python_envs:

$ source $WORKON_HOME/my_virtual_env_name/bin/activate

Install whatever Python packages your project requires, using either of the following two methods.

Method 1 - Install using pip from command line:

$ pip install python_package_name1
$ pip install python_package_name2

Method 2 - Install using a requests.txt file:

$ echo "python_package_name1" >> requests.txt
$ echo "python_package_name2" >> requests.txt
$ pip install -r ./requests.txt

Optionally, but highly recommended, install virtualenvwrapper. It contains useful commands to make working with virtual Python environments easier:

$ pip install virtualenvwrapper
$ source /usr/local/bin/virtualenvwrapper.sh

On Windows, install virtualenvwrapper using:

$ pip install virtualenvwrapper-win

Basic usage of virtualenvwrapper Create a new virtual environment:

$ mkvirtualenv my_virtual_env_name

List all virtual environments:

$ lsvirtualenv

Activate a virtual environment:

$ workon my_virtual_env_name

Delete a virtual environment (caution! this is irreversible!):

$ rmvirtualenv my_virtual_env_name

I hope this help!

MikeyE
  • 1,756
  • 1
  • 18
  • 37
4

To solve the problem do following:

First uninstall numpy

sudo pip uninstall numpy

Install numpy with --no-cache-dir option

sudo pip install --no-cache-dir numpy

And to specify any specific version e.g. 1.14.2

sudo pip install --no-cache-dir numpy==1.14.2
johndpope
  • 5,035
  • 2
  • 41
  • 43
GauravLuthra
  • 1,027
  • 9
  • 8
  • Cannot uninstall 'numpy'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall. – tong Sep 13 '18 at 07:22
  • Thanks for the answer, `--no-cache-dir` did the trick for me – Arka Mukherjee Feb 02 '23 at 00:15
3

This command solved my problem.

pip3 install --upgrade numpy 
Pallamolla Sai
  • 2,337
  • 1
  • 13
  • 14
2

I got the same issue with quaternion module. When updating modules with conda, the numpy version is not up^dated to the last one. If forcing update with pip command pip install --upgrade numpy + install quaternion module by pip install --user numpy numpy-quaternion, the issue is fixed. May be the issue is coming from the numpy version. Here the execution result:

Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import numpy as np
>>> print np.__version__
1.14.3
>>>

(base) C:\Users\jc>pip install --user numpy numpy-quaternion
Requirement already satisfied: numpy in d:\programdata\anaconda2\lib\site-packages (1.14.3)
Collecting numpy-quaternion
  Downloading https://files.pythonhosted.org/packages/3e/73/5720d1d0a95bc2d4af2f7326280172bd255db2e8e56f6fbe81933aa00006/numpy_quaternion-2018.5.10.13.50.12-cp27-cp27m-win_amd64.whl (49kB)
    100% |################################| 51kB 581kB/s
Installing collected packages: numpy-quaternion
Successfully installed numpy-quaternion-2018.5.10.13.50.12

(base) C:\Users\jc>python
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import numpy as np
>>> import quaternion
>>> 
1

I faced the same problem due to documentation inconsistencies. This page says the examples in the docs work best with python 3.x: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_intro/py_intro.html#intro , whereas this installation page has links to python 2.7, and older versions of numpy and matplotlib: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html

My setup was as such: I already had Python 3.6 and 3.5 installed, but since OpenCv-python docs said it works best with 2.7.x, I also installed that version. After I installed numpy (in Python27 directory, without pip but with the default extractor, since pip is not part of the default python 2.7 installation like it is in 3.6), I ran in this RuntimeError: module compiled against API version a but this version of numpy is error. I tried many different versions of both numpy and opencv, but to no avail. Lastly, I simply deleted numpy from python27 (just delete the folder in site-packages as well as any other remaining numpy-named files), and installed the latest versions of numpy, matplotlib, and opencv in the Python3.6 version using pip no problem. Been running opencv ever since.

Hope this saves somebody some time.

guru_laghima
  • 49
  • 1
  • 7
1

When all else fail, check with the following script and disable unwanted python import path(s), or upgrade the package on those paths:

python ./test.py

test.py content:

import numpy as np
print(f'numpy version:{np.__version__}')
import sys

from pprint import pprint

pprint(sys.path)

import tensorflow as tf
print(f'TensorFlow version: {tf.__version__}')

For my case, it was the outdated conda version in ~/.local/lib/python3.8/site-packages that was messing things up :(

biocyberman
  • 5,675
  • 8
  • 38
  • 50
1
pip install numpy -- user --upgrade --ignore-installed 

Worked for me.

Ophir Carmi
  • 2,701
  • 1
  • 23
  • 42
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch May 18 '23 at 20:41
0

For those using anaconda Python:

conda update anaconda
YakovK
  • 337
  • 2
  • 10
0

You might want to check your matplotlib version.

Somehow I installed a dev version of matplotlib which caused the issue. A downgrade to stable release fixed it.

One can also can try python -v -c 'import YOUR_PACKAGE' 2>&1 | less to see where the issue occurred and if the lines above error can give you some hints.

hlin
  • 1
0

You may also want to check your $PYTHONPATH. I had changed mine in ~/.bashrc in order to get another package to work.

To check your path:

    echo $PYTHONPATH

To change your path (I use nano but you could edit another way)

    nano ~/.bashrc

Look for the line with export PYTHONPATH ...

After making changes, don't forget to

   source ~/.bashrc
gtcoder
  • 151
  • 2
  • 6
0

I had the same error when trying to launch spyder. "RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa". This error appeared once I modified the numpy version of my machine by mistake (I thought I was in a venv). If your are using spyder installed with conda, my advice is to only use conda to manage package.

This works for me:

conda install anaconda

(I had conda but no anaconda on my machine) then:

conda update numpy
Luce Philibert
  • 157
  • 2
  • 2
  • 10
0

The below command worked for me :

conda install -c anaconda numpy
0

Although this question is very old, but I do believe there are still many facing similar problem as I did. I encountered the above reported error when I used Python3 in a Raspberry Pi micro-computer, which is running on Raspberry Pi OS.

This is perhaps due to missing some libraries when installed the Numpy module. I solved this problem following the suggestion in the Numpy website. Solutions for Numpy Module Import Error

This Numpy troubleshooting website is really informative and provides cross-platform solutions for Windows, Anaconda, Raspberry, etc. Perhaps, someone can first follow the suggestion in this Numpy official website in order to solve the error.

Eureka JX
  • 66
  • 3
0

I had same issue when I used import pyopencl and I did not want to upgrade numpy cause tensorflow requires old version of numpy so I solved it by simply:

python -m pip uninstall pyopencl && python -m pip install pyopencl

This way pyopencl was configured with existing numpy version and error solved.

Cyborg
  • 1,437
  • 19
  • 40
0

I suffered with this problem for a long time, firstly you have to upgrade numby then try this code :

import numpy as np 
print np.__version__ 

if gives you different version from the new one , uninstall the numpy(the new version) and use this

print numpy.__path__

go to that old numpy and delete the file , then install new version again

  • Hi Esraa The top-2 answers in this thread that say this, and duplicating answers is not recommended. When you reach [15 reputation](https://stackoverflow.com/help/privileges) you will be able to "vote up" helpful answers. Take a look at [how to answer](https://stackoverflow.com/help/how-to-answer) and find new, well-asked questions. – Alexander L. Hayes Jan 01 '23 at 01:58
-2

This works for me:

My pip is not work after upgrade, so the first thing I need to do is to fix it with

sudo gedit /usr/bin/pip

Change the line

from pip import main

to

from pip._internal import main

Then,

 sudo pip install -U numpy