How can I upgrade NumPy?
Asked Answered
P

14

91

When I installed OpenCV using Homebrew (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 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 question 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 the NumPy version to OpenCV?

Primordium answered 14/2, 2015 at 17:0 Comment(0)
P
44

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.

Check the path,

import numpy
print numpy.__path__

and manually delete it using rm.

Primordium answered 14/2, 2015 at 17:17 Comment(3)
It doesn't seem to be working for Mac OSX El Capitan, because (a) it refuses to delete the numpy's folder which is in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python, and (b) this is generally not a good approach,Bedrail
@ИванГеоргиев: How else, then?Parturifacient
Thank you so much, this solved an issue with conflicting versions that I had put far too much time into. Debian python and pip seem to live in /usr/lib, \usr/local/lib and ~/.local/lib/.Freiman
V
81

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
Vasyuta answered 28/2, 2018 at 0:54 Comment(0)
P
44

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.

Check the path,

import numpy
print numpy.__path__

and manually delete it using rm.

Primordium answered 14/2, 2015 at 17:17 Comment(3)
It doesn't seem to be working for Mac OSX El Capitan, because (a) it refuses to delete the numpy's folder which is in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python, and (b) this is generally not a good approach,Bedrail
@ИванГеоргиев: How else, then?Parturifacient
Thank you so much, this solved an issue with conflicting versions that I had put far too much time into. Debian python and pip seem to live in /usr/lib, \usr/local/lib and ~/.local/lib/.Freiman
R
14

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.

Ratline answered 6/1, 2016 at 18:19 Comment(4)
sudo easy_install numpy searched added the latest numpy path(already existing coz of my sudo pip install numpy). Perfect soln.Superphysical
Had similar issue. sudo easy_install numpy . It removed previous and updated latest. Traceback 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 Streeter
This worked for me as well (first installing it using pip --ignore-installed, then "linking" it so python finds it using sudo easy_install numpy). The other solutions didn't work.Tish
sudo easy_install numpy did not work for me, but sudo pip install --ignore-installed numpy did work nicely. Thanks BjornW.Purposely
J
11

Update numpy

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

note:

You need to check which python version are you using. pip for python 2.7+ or pip3 for python 3+

Jutta answered 30/7, 2019 at 11:17 Comment(0)
T
6

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.

Tellurate answered 19/1, 2017 at 6:15 Comment(0)
N
3

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.

Necrotomy answered 27/12, 2015 at 4:27 Comment(0)
T
3

Because you have multiple versions 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.

Trelliswork answered 3/2, 2017 at 7:41 Comment(0)
R
3

This works for me:

pip install numpy --upgrade
Robinrobina answered 22/10, 2017 at 12:59 Comment(0)
C
2

If you don't encounter any permission errors with

pip install -U numpy

try:

pip install --user -U numpy
Cherycherye answered 28/12, 2017 at 14:22 Comment(1)
I know this is a couple of years old, but awesome tyWateriness
G
1

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
Gonagle answered 13/3, 2017 at 15:11 Comment(0)
W
1

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
Warily answered 22/6, 2017 at 8:16 Comment(0)
T
1

If you are using multiple versions of Python (for example 3.8 and 3.9), then specify explicitly for which one you want to install and update numpy:

python3.8 -m pip install numpy --upgrade
# or 
python3.9 -m pip install numpy --upgrade

Then run your program with the appropriate version of Python.

Think answered 30/9, 2022 at 12:25 Comment(0)
S
0

pip install numpy --upgrade

You can try to use the ablove command and use as adminstrator if open CMD.

Somnambulation answered 14/2, 2015 at 17:0 Comment(1)
this same answer was already given in other answers, among them the top-voted oneLaxative
N
0

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.

The Anaconda installation worked like a charm:

After installation,

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

numpy 1.13.3 numpydoc 0.7.0

Nivernais answered 17/1, 2018 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.