When I installed OpenCV using Homebrew (brew
), I got this problem whenever I run this command to test python -c \"import cv2\"
:
Run
This works for me:
pip install numpy --upgrade
FYI, when you using or importing TensorFlow, a similar error may occur, like (caused by NumPy):
RuntimeError: module compiled against API version 0xa but this version of numpy is 0x9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 23, in <module>
from tensorflow.python import *
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 60, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
_pywrap_tensorflow = swig_import_helper()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
ImportError: numpy.core.multiarray failed to import
Error importing tensorflow. Unless you are using bazel,
you should not try to import tensorflow from its source directory;
please exit the tensorflow source tree, and relaunch your python interpreter
from there.
I followed Elmira's and Drew's solution, sudo easy_install numpy
, and it worked!
sudo easy_install numpy
Searching for numpy
Best match: numpy 1.11.3
Removing numpy 1.8.2 from easy-install.pth file
Adding numpy 1.11.3 to easy-install.pth file
Using /usr/local/lib/python2.7/dist-packages
Processing dependencies for numpy
Finished processing dependencies for numpy
After that I could use TensorFlow without error.
After installing pytorch, I got a similar error when I used:
import torch
Removing NumPy didn't help (I actually renamed NumPy, so I reverted back after it didn't work). The following commands worked for me:
sudo pip install numpy --upgrade
sudo easy_install numpy
The error you mentioned happens when you have two versions of NumPy on your system. As you mentioned, the version of NumPy you imported is still not upgraded since you tried to upgrade it through pip (it will upgrade the version existing in '/Library/Python/2.7/site-packages'
).
However Python still loads the packages from '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy'
where the pre-installed packages live.
In order to upgrade that version you have to use easy_install
. The other way around this problem is using virtualenv
and setting up a new environment with all the requirements you need.
For python 2
pip install numpy --upgrade
You would also needed to upgrade your tables as well for updated version of numpy. so,
pip install tables --upgrade
For python 3
pip3 install numpy --upgrade
Similarly, the tables for python3 :-
pip3 install tables --upgrade
You need to check which python version are you using. pip for python 2.7+ or pip3 for python 3+
All the same.
sudo easy_install numpy
My Traceback
Searching for numpy
Best match: numpy 1.13.0
Adding numpy 1.13.0 to easy-install.pth file
Using /Library/Python/2.7/site-packages
Processing dependencies for numpy