230

I'm trying to run this program

import cv2
import time

cv.NamedWindow("camera", 1)

capture = cv.CaptureFromCAM(0)

while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    if cv.WaitKey(10) == 27:
        break
cv.DestroyAllWindows()

But I'm having a problem with numpy, I'm using pyschopy along with opencv. The problem I keep getting is this error report:

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

Traceback (most recent call last):

File "C:\Users\John\Documents\EyeTracking\Programs\GetImage.py", line 1, in

import cv2

ImportError: numpy.core.multiarray failed to import

I have numpy-1.6.1-py2.7 in the psychopy folder, I'm just confused as to what is wrong?

Adriaan
  • 17,741
  • 7
  • 42
  • 75

35 Answers35

239

I was getting the same error and was able to solve it by updating my numpy installation to 1.8.0:

pip install -U numpy
David Ketcheson
  • 4,035
  • 2
  • 30
  • 25
  • 12
    I get the same error message and I have numpy-1.9.2 installed. – tommy.carstensen May 14 '15 at 15:13
  • @tommy.carstensen Hi, I got the same issue, I tried pip install -U numpy, it downloaded latest numpy version, and said that it is installed successully, but in fact still the old version – user824624 Dec 02 '15 at 22:11
  • Was in the exact same condition... For me the issue was dual installation of numpy which was in a conflicting version with other libraries and it was shadowing the correct numpy version installed by conda. Solution: pip uninstall numpy ... this removed the faulty numpy version and raised the correct numpy version back to life... – roy650 Jan 30 '17 at 19:48
  • 3
    For me, I needed to remove numpy, then install numpy. it worked. – jef Sep 14 '17 at 00:30
  • It's odd, but I ran into this error even when using numpy `1.20.3`, and fixed it by upgrading to numpy `1.21.0`. I wonder why this is still an issue – cowlinator Jun 22 '21 at 22:46
  • I get the feeling that upgrading NumPy to a newer verison is the solution, independent of the actual versions involved. – AstroFloyd Aug 22 '22 at 12:35
45

In the case that

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!

Note: For the command, use double underscore before and after 'version' and 'file'

Lucas Walter
  • 942
  • 3
  • 10
  • 23
alexfigtree
  • 1,516
  • 14
  • 16
  • hi, I met the same issue, could you instruct me how to replace the the numpy folder – user824624 Dec 02 '15 at 22:05
  • Hi, you'll have to look at the path given after the python -c command and find it on your computer. I'm sure you can rm the directory on OSX via terminal, but I did it manually and it solved my issue. – alexfigtree Jan 18 '16 at 21:29
  • AttributeError: 'module' object has no attribute 'file' – xApple May 09 '16 at 16:34
  • 1
    @xApple Add dunderscores (it means: double underscore) before and after _versions_ and _file_ – Hamlett Jun 09 '16 at 00:50
  • You can also simply try uninstalling the faulty numpy. Then check if you have a different numpy version which will "rise" (as I had) and if needed - then reinstall it. – roy650 Jan 30 '17 at 19:55
  • This solved the problem for me. I am wondering if there's a slick way to clean up, finding all the packages like this, old versions in ~/.local/lib and new in /usr/lib, without manually monkeying with commands like 'rm -rf' and making mistakes? Can pip do that? – DarenW Sep 11 '19 at 18:22
  • BTW, print needs ( and ) around whatever it's being told to print. – DarenW Sep 11 '19 at 18:23
30

If you are using python3, the following command fixes the problem on MacOS:

python3 -m pip install numpy -I

-I in detail:

pip install --help

-I, --ignore-installed      Ignore the installed packages, overwriting them.
abe
  • 355
  • 2
  • 9
Bharanidharan K
  • 679
  • 1
  • 9
  • 11
  • 1
    This worked but I used python -m pip install numpy -I – Ahmad Moussa Oct 11 '19 at 05:39
  • 12
    Please explain what a flag does, otherwise devs end up propagating magic recipes – P i Nov 24 '21 at 09:16
  • 1
    as per `pip`'s help: `-I`, `--ignore-installed` Ignore the installed packages, overwriting them. This can break your system if the existing package is of a different version or was installed with a different package manager! – emil Jan 10 '22 at 09:05
27

If you would like to install a specific version:

pip install numpy==1.8
blackraven
  • 5,284
  • 7
  • 19
  • 45
ril3y
  • 912
  • 4
  • 10
  • 19
18

Try sudo pip install numpy --upgrade --ignore-installed.

It work in Mac OS 10.11.

You should close The 'Rootless' if above shell isn't work.

Startry
  • 574
  • 1
  • 4
  • 17
14
pip install opencv-python==3.4.2.17 numpy==1.14.5

did the job for me!

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Bendy Latortue
  • 391
  • 5
  • 6
11

you may need upgrade pip, it works for me

pip install --upgrade pip
pip install -U numpy
TURUI
  • 111
  • 1
  • 2
11

I had the same error message, after trying some of the suggested solutions without success, I found that I needed to run:

pip uninstall numpy

multiple times. Each time several different files were flagged for removal, from different versions of numpy that had accumulated on my system.

Once there was nothing left to uninstall, it was a matter of

pip install numpy

Hope this helps someone!

bio_james
  • 161
  • 2
  • 2
10

All these varied answers suggest that many different problems appear as ImportError: numpy.core.multiarray failed to import.

==> So look for more error information before the stack traceback

E.g. steps to reproduce one such problem (these steps worked fine until numpy 1.20 was released):

# set up a virtualenv
pyenv virtualenv 3.8.7 pip-issue-9542
pyenv local pip-issue-9542

# update pip, setuptools
pip install -U pip setuptools

# install 2 pips
pip install --no-cache-dir numpy==1.19.5 cvxpy==1.1.3

# try to load cvxpy
python -c 'import cvxpy'

Output:

RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/var/pyenv/versions/pip-issue-9542/lib/python3.8/site-packages/cvxpy/__init__.py", line 18, in <module>
    from cvxpy.atoms import *
  File "/usr/local/var/pyenv/versions/pip-issue-9542/lib/python3.8/site-packages/cvxpy/atoms/__init__.py", line 20, in <module>
    from cvxpy.atoms.geo_mean import geo_mean
  File "/usr/local/var/pyenv/versions/pip-issue-9542/lib/python3.8/site-packages/cvxpy/atoms/geo_mean.py", line 20, in <module>
    from cvxpy.utilities.power_tools import (fracify, decompose, approx_error, lower_bound,
  File "/usr/local/var/pyenv/versions/pip-issue-9542/lib/python3.8/site-packages/cvxpy/utilities/power_tools.py", line 18, in <module>
    from cvxpy.atoms.affine.reshape import reshape
  File "/usr/local/var/pyenv/versions/pip-issue-9542/lib/python3.8/site-packages/cvxpy/atoms/affine/reshape.py", line 18, in <module>
    from cvxpy.atoms.affine.hstack import hstack
  File "/usr/local/var/pyenv/versions/pip-issue-9542/lib/python3.8/site-packages/cvxpy/atoms/affine/hstack.py", line 18, in <module>
    from cvxpy.atoms.affine.affine_atom import AffAtom
  File "/usr/local/var/pyenv/versions/pip-issue-9542/lib/python3.8/site-packages/cvxpy/atoms/affine/affine_atom.py", line 22, in <module>
    from cvxpy.cvxcore.python import canonInterface
  File "/usr/local/var/pyenv/versions/pip-issue-9542/lib/python3.8/site-packages/cvxpy/cvxcore/python/__init__.py", line 3, in <module>
    import _cvxcore
ImportError: numpy.core.multiarray failed to import

The important clue is RuntimeError: module compiled against API version 0xe but this version of numpy is 0xd.

Searching on that can get you to pip issue 9542 and cvxpy issue 1229 which explain what went wrong.

The workarounds in this case are to (a) update to cvxpy>=1.1.10, or (b) update to numpy>=1.20, or (c) update both. You'll have to cope with library changes since the original environment is not reproducible without diving into pip's --no-build-isolation world.

Jerry101
  • 12,157
  • 5
  • 44
  • 63
9

OK so I found a solution that worked for me when trying to get OpenCV to work with Python 3.9 on Windows 10.

This is a known issue for Windows versions past v2004.

In short, the version of NumPY that you need is v1.19.3:

pip uninstall numpy

pip install numpy==1.19.3

Or to do this in one command, use the --force-reinstall flag for pip:

pip install --force-reinstall numpy==1.19.3
dope
  • 156
  • 2
  • 6
7

I used Anaconda environment and had the same issue. I tried all the aforementioned approaches and, alas, it didn't help me. Accumulated the suggestions, here the way which helped me:

  1. Delete all NumPy folders in the virtual environment or in the system if you don't use a virtual environment, for example in my case:

    ~/home/anaconda3/envs//lib/python/site-packages/numpy

    ~/home/anaconda3/envs//lib/python/site-packages/numpy.libs

    ~/home/anaconda3/envs//lib/python/site-packages/numpy-.dist-info

  2. Install new Numpy with:

    pip install numpy -U

Hope, it could help in the same case

Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38
6

for me this error came up when installing pygrib with conda and importing it.

conda install -c conda-forge numpy

solved the problem.

Oliver Wilken
  • 2,654
  • 1
  • 24
  • 34
  • this solved the problem for me when I got the mentioned error during "import tensorflow" – Mona Jalal Mar 25 '18 at 07:50
  • It solved my problem when I use opencv, Anaconda python should use this method, pip or pip3 will make things even worse. – raidsan Aug 29 '19 at 12:10
5
pip install numpy==1.19.3

this did the job for me.

Bickky Sahani
  • 351
  • 4
  • 13
4

In my case this problem was because I'd two python installations (2.7 and 3.5) and pip was installing numpy in the 3.5 python directory only, irrespective of which pip version I used.

I solved the problem by explicitly specifying the target install directory as such:

pip install --target c:\apps\python-2.7\Lib\site-packages numpy
Vikash Madhow
  • 1,287
  • 11
  • 15
3

I was getting the same error and the problem was solved by updating my numpy installation from 1.7.1 to 1.12.1

pip install -U numpy

The followings were my cmd sequence when the error was occurred, slightly different from the above:

$ python

Python 2.7.12 |Anaconda 4.2.0 (x86_64)| (default, Jul  2 2016, 17:43:17) 

[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

Anaconda is brought to you by Continuum Analytics.

Please check out: http://continuum.io/thanks and https://anaconda.org

>>> import cv2

>>> import numpy as np

>>> from matplotlib import pyplot as plt
muru
  • 4,723
  • 1
  • 34
  • 78
Rider44
  • 141
  • 3
  • 8
2

After having a nightmare using the pip install -U numpy several months ago, I gave up. I went through installing CV2s and opencv without success.

I was using numpy ver 1.9.1 on python34 and the upgrade just kept stalling on on 1.9.

So I went to https://pypi.python.org/pypi/numpy and discovered the latest numpy version for my python3.4.

I downloaded the .whl file and copied it into the folder containing my python installation, C:\Python34, in my case.

I then ran pip intall on the file name and I can now import cv2 problem free.

Make sure you close down python before you start, obvious but essential

Stephen O
  • 31
  • 2
2

The same error came for me. The problem is that you might have created a file called numpy.py. This file might coincide with numpy library. So, delete that numpy.py file and the problem gets solved.

Manoj Guha
  • 189
  • 1
  • 4
2

It worked for me. So you can try following command

$ pip install numpy -I
Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
2

I don't really understand this error but I solved this error with below.

pip install -U opencv-python

Hope it was useful!

Saeed
  • 3,294
  • 5
  • 35
  • 52
2

In my case installing from apt solved my problem.

You can try uninstall it from pip and install from apt (if you are using ubuntu etc.)

pip3 uninstall numpy
sudo apt-get install python3-numpy
fakturk
  • 415
  • 7
  • 16
2

run this codes worked for me, seems to be issue with version.

pip uninstall numpy

pip install numpy==1.19.3
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
MwarukaSon
  • 297
  • 4
  • 9
1

I had the same error after installing python and opencv in my D: drive (C: runs on a SSD). The problem seemed to be that my execution path was inside the numpy folder. You can check if that's the issue with this code :

import numpy
numpy.version.version

In case it retrieves any error related to the execution path, execute your original code from elsewhere. That solved the could not load numpy.core.multiarray error for me

egeres
  • 77
  • 3
  • 9
1

I had the same issue, and here's how it's solved in my case.

I tried pip install -U numpy but it didn't upgrade numpy, but conda install worked for me

ImportError: numpy.core.multiarray failed to import
admin@MacBook-Air$ pip install -U numpy
Requirement already up-to-date: numpy in /Users/admin/anaconda/lib/python2.7/site-packages
admin@MacBook-Air$ python
Python 2.7.12 |Anaconda 2.4.0 (x86_64)| (default, Jul  2 2016, 17:43:17) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import numpy
numpy.version.version
>>> numpy.version.version
'1.7.1'
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>> 
admin@MacBook-Air$ 
admin@MacBook-Air$ conda install numpy
Fetching package metadata .......
Solving package specifications: ..........

Package plan for installation in environment /Users/admin/anaconda:

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    scikit-learn-0.18.1        |      np111py27_0         4.9 MB

The following packages will be UPDATED:

    numexpr:      2.3.0-np17py27_0  --> 2.6.1-np111py27_1 
    numpy:        1.7.1-py27_2      --> 1.11.2-py27_0     
    scikit-learn: 0.14.1-np17py27_1 --> 0.18.1-np111py27_0
    scipy:        0.13.2-np17py27_1 --> 0.18.1-np111py27_0

Proceed ([y]/n)? y

Fetching packages ...
scikit-learn-0 100% |#################################################################| Time: 0:00:16 312.60 kB/s
Extracting packages ...
[      COMPLETE      ]|####################################################################################| 100%
Unlinking packages ...
[      COMPLETE      ]|####################################################################################| 100%
Linking packages ...
[      COMPLETE      ]|####################################################################################| 100%
Nebulosar
  • 1,727
  • 3
  • 20
  • 46
Nutcracker
  • 359
  • 2
  • 8
1

I Had the same error occurring as I was using the numpy version suggested by the requirements.txt in the repo. When I tried to 'import pandas as pd' this error occurred. Then the solution was to upgrade numpy version to 1.15.2 as the version suggested in the requirements was mismatching with pandas. I uninstalled the existing numpy version with pip and reinstalled the new version.

pip install numpy==1.15.2 

Hope this helps someone

shan89
  • 11
  • 2
1

Tilde folders

In the event pip uninstall numpy and reinstallation of Numpy does not work. Review your site-packages folder for sub-folders beginning with a tilde ~

These folders relate to pip installations that got mangled and the installation was aborted part way through. The tilde folders were only ever meant to be tmp folders but ended up becoming permanent. In my case there was a file called ~mpy which was a mangled legacy Numpy folder. This led to compatibility issues and ImportErrors.

These mangled folders can safely be deleted, for further details see this answer

EQNX
  • 131
  • 10
1

I was able to solve the problem by updating my python to 3.8. I am using Macbook Air with Catalina. The problem started for me after updating TensorFlow. After updating it, the error doesn't disappear after I uninstalled and installed numpy several times.

Finally, I downloaded the python3.8 package from python website and installed. Then I installed numpy and it worked.

R Nanthak
  • 344
  • 3
  • 17
1

I got this same error in a conda environment, only six+ years later. The other responses were helpful, and eventually I tracked it down to this problem:

> conda list numpy
# packages in environment at [Anaconda3]:
#
# Name                    Version                   Build  Channel
numpy                     1.14.5                   pypi_0    pypi
numpy-base                1.18.1           py36hc3f5095_1
numpydoc                  0.9.1                    pypi_0    pypi

The problem was that 'numpy' was a different version than 'numpy-base'. I solved this with:

> conda install numpy-base=1.14.5

so the two of them matched each other.

Lucian
  • 461
  • 5
  • 6
0

This helped me

sudo pip3 install --upgrade --target=/home/<username>/.local/lib/python3.6/site-packages numpy
0

For me it was a two part. First:

pip uninstall numpy

Then @Oli Blum answer (thank you):

conda install -c conda-forge numpy

That did the trick for me

0

uninstall existing numpy and install opencv-python will resolve the issue

0

Just fixed this issue. import c2 or import numpy was not working. Uninstalled the most current version of numpy. Tried to install numpy==1.15.2 just like specified above, did not work. Tried numpy==1.19.3 IT worked. I guess not all versions work perfectly with all versions of python and dependencies. So keep uninstalling and install one that works.

0

In case you uninstalled and reinstalled or installed update but it still not work, especially if you're packaging with cx_freeze:

  • Try to change your code by import numpy before import cv2.
phnghue
  • 1,578
  • 2
  • 10
  • 9
0

I was struggling with this error in my Raspberry 4. I was developing a Django backend that used opencv.

What solved my problem was:

sudo pip install numpy==1.23.0

Raspberry info: Linux raspberrypi 5.15.61-v7l+

Python version: 3.9

Hope this helps someone.

0

The problem with the other answers here is that they want you to essentially upgrade some version of your packages (i.e. pip install --upgrade numpy will upgrade your numpy to the latest)... but what if you have a hard requirement to stay at the current version you are already at?

Some of the answers will work, but only if you separately install the numpy dependencies after the rest of your dependencies. If you are attempting to set this up from scratch (i.e with python3 -m pip install -r requirements.txt in a new virtual environment), you will run into this error until you also run python3 -m pip install numpy==<the older version of numpy you need>, after having initially installed your dependencies. Which essentially installs some dependencies twice or needlessly separates them into two commands, and is not ideal.

When this happens, the root issue is that some package in the pip cache causes the incorrect mixture of C compile libs... and that will keep happening on every new install, until you clear the pip cache...

If you run python3 -m pip install --no-cache-dir -r requirements.txt (with your numpy defined in the requirements.txt), and that works, but it does not after removing the --no-cache-dir option, then the cache dir is the problem.

Simply run python3 -m pip cache purge to clear the wheel packages, then re-run python3 -m pip install -r requirements.txt. Now the packages will install correctly on the first try every time.

Jon
  • 952
  • 1
  • 11
  • 17
-1

Encountered this when trying to import Pytorch. Solved it by uninstalling or removing numpy repeatedly until no version was left on my Ubuntu and installing the newest version. In my case pip encountered Access is denied permission errors all the time maybe because of conflict with conda.

Run sudo pip uninstall numpy. If it refuses to delete the package from a <location> (make sure to make it the location of that specific package, not all packages - this is irreversible), run sudo rm -r <location>. Repeat above until the first command doesn't do anything and import numpy can't find the package within Python shell.

Install numpy using pip.

Michał Gacka
  • 2,935
  • 2
  • 29
  • 45