How can I upgrade numpy?

2019-01-08 16:47发布

问题:

When I installed OpenCV using brew, I got this problem whenever I run this command to test 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 the numpy, but this is confusing

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

When I run brew to upgrade numpy, i got this problem.

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 thread and deleted anaconda from my mac.

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 numpy version to opencv ?

回答1:

Because we have two numpy in the system. One is install by brew and second is install by pip. So in order to solve the problem, we need to delete one and use the default numpy install by opencv.

Check the path

import numpy
print numpy.__path__

and manually delete it using rm



回答2:

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 exist 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.



回答3:

When you already have an older version of numpy, use this:
pip install numpy --upgrade.

If it still doesn't work, try:
pip install numpy --upgrade --ignore-installed



回答4:

FYI,

When you using or importing Tensorflow, similar error may occurred 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.



回答5:

I tried doing sudo pip uninstall numpy instead, because the rm didn't work at first.

Hopefully that helps.

Uninstalling then to install it again.



回答6:

Because you have multiple version of numpy installed.

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

Then pip install numpy will get you the newest version of numpy.



回答7:

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

import torch

Removing numpy didn't help (I actually renamed numpy, so reverted back after it didn't work). The following commands worked with me:

sudo pip install numpy --upgrade
sudo easy_install numpy


回答8:

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


回答9:

pip install numpy --upgrade

It works for me



回答10:

If you encounter no permission error with
pip install -U numpy
try
pip install --user -U numpy



回答11:

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

The anaconda installation worked like a charm.

https://www.scipy.org/install.html

https://www.anaconda.com/download/

After installation,

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

numpy 1.13.3 numpydoc 0.7.0