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

167,924

Solution 1

upgrade numpy to the latest version

pip install numpy --upgrade

Solution 2

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.

Solution 3

This worked for me:

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

Solution 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

Solution 5

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
Share:
167,924
Isaiah Nields
Author by

Isaiah Nields

Updated on July 08, 2022

Comments

  • Isaiah Nields
    Isaiah Nields almost 2 years

    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?

  • Jakobovski
    Jakobovski about 8 years
    Becareful! It might not be a good idea for you to install multiple versions of python. You may end up like this guy: stackoverflow.com/questions/14117945/…
  • Marcel Colomb
    Marcel Colomb almost 8 years
    Worked for me as well. Thank you
  • MAZDAK
    MAZDAK almost 7 years
    This solves the problem, but WHY? This problem arises because there are two numpy installed. How does upgrade solve this issue?!
  • Guillaume Chevalier
    Guillaume Chevalier almost 7 years
    This does not fix for me: Requirement already up-to-date: numpy in /home/ubuntu/miniconda/lib/python3.6/site-packages
  • Guillaume Chevalier
    Guillaume Chevalier almost 7 years
    Worked, but needed to reinstall it with pip install numpy -I (the capital i argument says to install while ignoring the current installation which got moved).
  • Semih Korkmaz
    Semih Korkmaz over 6 years
    It might solve the problem if you import torch using your default python, which has an older version of numpy.
  • dinosaur
    dinosaur over 6 years
    this worked for me when trying to import newly installed pytorch with import torch
  • Seabass77
    Seabass77 over 6 years
    This worked for me after uninstalling and reinstalling python
  • Amit Singh
    Amit Singh about 6 years
    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
  • ajspencer
    ajspencer almost 6 years
    The document you linked to gives a 404 error. Could you update the link?
  • MikeyE
    MikeyE almost 6 years
    @user3243242 As you wish! :-)
  • tong
    tong over 5 years
    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.
  • Anmol Saraf
    Anmol Saraf over 5 years
    wow. it worked !!!! Never thought the solution could be so easy. Thanks :)
  • ComputerScientist
    ComputerScientist almost 3 years
    Don't do sudo pip install !
  • tarmas99
    tarmas99 over 2 years
    Similar thing happened with me in Google Colab with a package called clusterval. @GuillaumeChevalier 's comment helped me solve this.