Search code examples
macospython-2.7opencvnumpyosx-elcapitan

Cannot import open cv2 in python. Error: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel [[Cannot upgrade numpy ]]


I and trying to build something on python package cv2. My os is Mac OS X El Capitan and python version is python 2.7.10.

But when I import cv2 as

>>> import cv2

I get following error.

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>
ImportError: numpy.core.multiarray failed to import

I figured out that it is a problem of numpy version and I need to upgrade it. So I tried

sudo pip install numpy --upgrade

But system threw following error:

OSError: [Errno 1] Operation not permitted: '/tmp/pip-JSOF8d-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy-1.8.0rc1-py2.7.egg-info'

Then I tried to install a particular version of numpy (required version) using following command.

pip install numpy==1.8

Even this didn't work and I got following traceback error:

SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel.

Now I am clueless, how to proceed. Please help me to figure this out.

Thanks in advance.


Solution

  • I was also facing the same issue, 2 things which solved my problem are

    1. Run

      pip install --ignore-installed numpy

    2. run the above command in terminal, that is don't use setuptools.

    But, it will upgrade to latest version of numpy, hope this resolves your problem. Its been quite tough for me to fix couple of el-capitan issues