19

Trying to install OpenCV and running into an issue where attempting to import cv2 results in this output -

    RuntimeError: module compiled against API version 9 but this version of numpy is 7

Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
import cv2
ImportError: numpy.core.multiarray failed to import

I'm running on Windows 7 x64, Python v 2.7.9 Thanks!

NoTrueScotsman
  • 305
  • 1
  • 3
  • 10
  • How did you install numpy and opencv? – ali_m Jan 26 '15 at 21:02
  • I followed the instructions here for using pre-built binaries: http://docs.opencv.org/trunk/doc/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html#install-opencv-python-in-windows – NoTrueScotsman Jan 27 '15 at 04:02
  • 2
    Possible duplicate of [ImportError: numpy.core.multiarray failed to import](http://stackoverflow.com/questions/20518632/importerror-numpy-core-multiarray-failed-to-import) – Hamlett Jun 09 '16 at 00:56

7 Answers7

19

The error is telling you that you have an out of date version of numpy. If you used pip to install things you can simply run pip install numpy -U, or download the appropriate version from their website.

chris
  • 4,840
  • 5
  • 35
  • 66
3

In case

pip install -U numpy

doesn't work (even with sudo), you may want to make sure you're using the right version of numpy. I had the same "numpy.core.multiarray failed to import" issue, but it was because I had 1.6 installed for the version of Python I was using, even though I kept installing 1.8 and assumed it was installing in the right directory.

I found the bad numpy version by using the following command in my Mac terminal:

python -c "import numpy;print numpy.version;print numpy.file";

This command gave me the version and location of numpy that I was using (turned out it was 1.6.2). I went to this location and manually replaced it with the numpy folder for 1.8, which resolved my "numpy.core.multiarray failed to import" issue. Hopefully someone finds this useful!

alexfigtree
  • 1,516
  • 14
  • 16
1

I had a similar problem and I solved it by downgrading my numpy version.

What I did was:

pip install opencv-python
pip uninstall numpy
pip install numpy=1.18

This has worked for me using

Python 3.7 opencv-python 4.4.0.46 numpy 1.18.0

AlbertoIzq
  • 11
  • 2
0

linux: sudo apt-get install python-numpy

phil294
  • 10,038
  • 8
  • 65
  • 98
  • 1
    okay. but the other proposed solutions didn't work for me on linux, so I thought it might be worth sharing :) – phil294 Mar 08 '16 at 12:31
0

if you are using ubuntu bionic beaver then try running: sudo apt-get install python-numpy had the same issue, resolve by running the above command. Hope it helps

Ahmed J.
  • 484
  • 6
  • 11
0

In your environment you can try this command:

conda uninstall numpy

conda install -c conda-forge numpy

yasi
  • 397
  • 1
  • 4
  • 14
0

I use Python 3.7 @ RPI 4.

For opencv to install properly I had to install the listed libraries below. (Not every package was actually installed, after request)

Regarding Numpy, I think one should stick to the latest version.

For me what worked is to uninstall the existing version 1.16.2 and stick with the current stable 1.21.2.

Stackoverflow topic at missing libraries here: ImportError: libcblas.so.3: cannot open shared object file: No such file or directory.

gsoqp
  • 1