当我使用 Homebrew (brew
) 安装 OpenCV 时,每当我运行此命令测试 python -c "import cv2"
时都会遇到此问题:
RuntimeError: module compiled against API version 9 but this version of numpy is 6
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: numpy.core.multiarray failed to import
我试图升级 NumPy,但这令人困惑:
>>> import numpy
>>> print numpy.__version__
1.6.1
当我运行 brew 升级 NumPy 时,我遇到了这个问题:
brew install -u numpy
Warning: numpy-1.9.1 already installed
当我卸载它时:
sudo pip install numpy
Requirement already satisfied (use --upgrade to upgrade): numpy in ./anaconda/lib/python2.7/site-packages
我已关注 this question 并从我的 mac 中删除了 Anaconda。
pip install numpy
Requirement already satisfied (use --upgrade to upgrade): numpy in /Library/Python/2.7/site-packages
但一切都没有改变。如何将 NumPy 版本链接到 OpenCV?
如果您已经拥有旧版本的 NumPy,请使用以下命令:
pip install numpy --upgrade
如果还是不行,试试:
pip install numpy --upgrade --ignore-installed
因为我们在系统中有两个 NumPy 安装。一个由 Homebrew 安装,第二个由 pip 安装。所以为了解决这个问题,我们需要删除一个并使用OpenCV默认安装的NumPy。
检查路径,
import numpy
print numpy.__path__
并使用 rm
手动删除它。
您提到的错误发生在您的系统上有两个版本的 NumPy 时。正如您所提到的,您导入的 NumPy 版本仍未升级,因为您尝试通过 pip 升级它(它将升级 '/Library/Python/2.7/site-packages'
中现有的版本)。
但是,Python 仍会从预安装包所在的 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy'
加载包。
要升级该版本,您必须使用 easy_install
。解决此问题的另一种方法是使用 virtualenv
并设置具有您需要的所有要求的新环境。
sudo easy_install numpy
搜索添加了最新的 numpy 路径(我的 sudo pip install numpy
已经存在)。完美的解决方案。
sudo easy_install numpy
。它删除了以前的并更新了最新的。回溯 Searching for numpy Best match: numpy 1.11.2 Removing numpy 1.8.2 from easy-install.pth file Adding numpy 1.11.2 to easy-install.pth file
更新 numpy
对于蟒蛇 2
pip install numpy --upgrade
您还需要升级表以及更新版本的 numpy。所以,
pip install tables --upgrade
对于蟒蛇 3
pip3 install numpy --upgrade
同样,python3 的表:-
pip3 install tables --upgrade
笔记:
您需要检查您使用的是哪个 python 版本。 python 2.7+ 的 pip 或 python 3+ 的 pip3
仅供参考,当您使用或导入 TensorFlow 时,可能会出现类似的错误,例如(由 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.
我遵循了 Elmira 和 Drew 的解决方案 sudo easy_install numpy
,它工作!
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
之后,我可以毫无错误地使用 TensorFlow。
我尝试改用 sudo pip uninstall numpy
,因为 rm
起初不起作用。
希望这会有所帮助。
卸载然后重新安装。
因为您安装了多个版本的 NumPy。
尝试 pip uninstall numpy
和 pip list | grep numpy
几次,直到您看不到 pip list | grep numpy
的输出。
然后 pip install numpy
将为您提供最新版本的 NumPy。
这对我有用:
pip install numpy --upgrade
如果您没有遇到任何权限错误
pip install -U numpy
尝试:
pip install --user -U numpy
全部都一样。
sudo easy_install numpy
我的追溯
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
安装pytorch后,我在使用时遇到了类似的错误:
import torch
删除 NumPy 并没有帮助(我实际上重命名了 NumPy,所以在它不起作用后我又恢复了)。以下命令对我有用:
sudo pip install numpy --upgrade
sudo easy_install numpy
如果您被困在没有 root 访问权限的机器上,那么最好处理自定义 Python 安装。
Anaconda 的安装就像一个魅力:
安装包 (SciPy.org)
Anaconda Python/R 发行版 - 下载
安装后,
[bash]$ /xxx/devTools/python/anaconda/bin/pip 列表 --format=columns | grep numpy numpy 1.13.3 numpydoc 0.7.0
不定期副业成功案例分享
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
中的 numpy 文件夹,并且 (b) 这通常不是一个好方法,/usr/lib
、\usr/local/lib
和~/.local/lib/
中。