Python 如何升级 NumPy?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28517937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:21:35  来源:igfitidea点击:

How can I upgrade NumPy?

pythonopencvnumpyhomebrewanaconda

提问by user3378649

When I installed OpenCV using Homebrew(brew), I got this problem whenever I run this command to test python -c "import cv2":

当我使用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

I tried to upgrade NumPy, but this is confusing:

我尝试升级 NumPy,但这令人困惑:

>>> import numpy
>>> print numpy.__version__
1.6.1

When I run brew to upgrade NumPy, I got this problem:

当我运行 brew 来升级 NumPy 时,我遇到了这个问题:

brew install -u numpy
Warning: numpy-1.9.1 already installed

When I uninstalled it:

当我卸载它时:

sudo pip install numpy
Requirement already satisfied (use --upgrade to upgrade): numpy in ./anaconda/lib/python2.7/site-packages

I have followed this questionand deleted Anacondafrom my mac.

我已经关注了这个问题并从我的 mac 中删除了Anaconda

pip install numpy
Requirement already satisfied (use --upgrade to upgrade): numpy in /Library/Python/2.7/site-packages

But nothing have changed. How can I link the NumPy version to OpenCV?

但一切都没有改变。如何将 NumPy 版本链接到 OpenCV?

采纳答案by user3378649

Because we have two NumPy installations in the system. One is installed by Homebrew and the second is installed by pip. So in order to solve the problem, we need to delete one and use the default NumPy install by OpenCV.

因为我们系统中有两个 NumPy 安装。一个由 Homebrew 安装,第二个由 pip 安装。所以为了解决这个问题,我们需要删除一个并使用OpenCV默认的NumPy安装。

Check the path,

检查路径,

import numpy
print numpy.__path__

and manually delete it using rm.

并使用rm.

回答by 0bserver07

I tried doing sudo pip uninstall numpyinstead, because the rmdidn't work at first.

我试着做sudo pip uninstall numpy,因为一rm开始没有用。

Hopefully that helps.

希望这有帮助。

Uninstalling then to install it again.

卸载然后重新安装。

回答by Elmira

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').

当您的系统上有两个版本的 NumPy 时,就会发生您提到的错误。正如您所提到的,您导入的 NumPy 版本仍未升级,因为您尝试通过 pip 升级它(它将升级 中现有的版本'/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.

然而,Python 仍然从'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy'预安装的包所在的位置加载包。

In order to upgrade that version you have to use easy_install. The other way around this problem is using virtualenvand setting up a new environment with all the requirements you need.

为了升级该版本,您必须使用easy_install. 解决此问题的另一种方法是使用virtualenv和设置具有您需要的所有要求的新环境。

回答by Jaeyoung Lee

FYI, when you using or importing TensorFlow, a similar error may occur, like (caused by NumPy):

仅供参考,当您使用或导入 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.

I followed Elmira's and Drew's solution, sudo easy_install numpy, and it worked!

我遵循了 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

After that I could use TensorFlow without error.

之后,我可以毫无错误地使用 TensorFlow。

回答by Xing Shi

Because you have multiple versions of NumPy installed.

因为您安装了多个版本的 NumPy。

Try pip uninstall numpyand pip list | grep numpyseveral times, until you see no output from pip list | grep numpy.

尝试pip uninstall numpypip list | grep numpy几次,直到你看到没有从输出pip list | grep numpy

Then pip install numpywill get you the newest version of NumPy.

然后pip install numpy将为您提供最新版本的 NumPy。

回答by vivkul

After installing pytorch, I got a similar error when I used:

安装pytorch后,使用的时候也出现类似的错误:

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:

删除 NumPy 没有帮助(我实际上重命名了 NumPy,所以在它不起作用后我又恢复了)。以下命令对我有用:

sudo pip install numpy --upgrade
sudo easy_install numpy

回答by Luo Bo

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

回答by Luna Kong

This works for me:

这对我有用:

pip install numpy --upgrade

回答by abaro

If you don't encounter any permission errors with

如果您没有遇到任何权限错误

pip install -U numpy

try:

尝试:

pip install --user -U numpy

回答by 147.3k

If you are stuck with a machine where you don't have root access, then it is better to deal with a custom Python installation.

如果您使用的机器没有 root 访问权限,那么最好处理自定义 Python 安装。

The Anaconda installation worked like a charm:

Anaconda 的安装效果非常好:

After installation,

安装后,

[bash]$ /xxx/devTools/python/anaconda/bin/pip list --format=columns | grep numpy

numpy 1.13.3 numpydoc 0.7.0

[bash]$ /xxx/devTools/python/anaconda/bin/pip list --format=columns | grep numpy

numpy 1.13.3 numpydoc 0.7.0