21

I used the following command to know the numpy version I am using

pip show numpy

output shown below

---
Name: numpy
Version: 1.8.2
Location: /usr/lib/python2.7/dist-packages
Requires: 

However when I am running matplotlib, I got a error as

RuntimeError: module compiled against API version a but this version of numpy is 9
        from matplotlib import pyplot as plt
      File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 27, in <module>
        import matplotlib.colorbar
      File "/usr/local/lib/python2.7/dist-packages/matplotlib/colorbar.py", line 32, in <module>
        import matplotlib.artist as martist
      File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 12, in <module>
        from .transforms import Bbox, IdentityTransform, TransformedBbox, \
      File "/usr/local/lib/python2.7/dist-packages/matplotlib/transforms.py", line 39, in <module>
        from matplotlib._path import (affine_transform, count_bboxes_overlapping_bbox,

I tried to upgrade numpy,

pip install numpy --upgrade

it shows to be installed successfully, but numpy still shows to be 1.8.2 and error continues to exist when running matplotlib.

I thought to uninstall numpy and reinstall it, the system gives the message saying

Not uninstalling numpy at /usr/lib/python2.7/dist-packages, owned by OS

how to solve it ? any idea about

RuntimeError: module compiled against API version a but this version of numpy is 9

user824624
  • 7,077
  • 27
  • 106
  • 183
  • 2
    you should use **virtualenv** and try this same thing in your environment, is not a good idea to mess up with your system's libraries – gerosalesc Dec 02 '15 at 20:01
  • @GermanRosales I don't think virtualenv is good for me, my ubuntu system has too many works and libraries depending on numpy. My numpy is working well at least yesterday. don't know why it doesn't work now. how would people normally upgrade numpy in ubuntu ? – user824624 Dec 02 '15 at 20:25
  • @user824624 I don't see how **virtualenv** can hurt anybody, but hey if what you want is to rescue your system's **numpy** I would try **apt-get** but only if you know what you are doing there. – gerosalesc Dec 02 '15 at 21:00
  • @GermanRosales currently I have matplotlib can't work with numpy, how could I use virtualenv to install numpy and make the current matplotlib work with new numpy in virtualenv – user824624 Dec 02 '15 at 21:22
  • so did you try **apt-get** to reinstall numpy? – gerosalesc Dec 03 '15 at 15:29
  • Having root access: Success with: sudo pip install numpy --upgrade – Helder Velez Nov 04 '17 at 22:22

6 Answers6

15

How can I upgrade numpy? might be working for you. IN that case it was a path problem:

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 

Solution:

Check the path

import numpy
print numpy.__path__

and manually delete it using rm

Community
  • 1
  • 1
cattt84
  • 941
  • 1
  • 10
  • 17
6

I also had the same problem until I came across this Issue on Pytorch github repository. This command worked perfectly for me:

pip install numpy -I

It is also mentioned there that pip install numpy --upgrade is not working(don't know why). You can check the above mentioned link.

Mukul
  • 310
  • 1
  • 6
  • 13
5

I had a similar problem with numpy when running torch. I tried uninstalling numpy and installing it using -U but it didn't work. After some search, I found this link and it solved my problem. It says you should change your numpy version.

pip uninstall numpy
pip install numpy==1.19.3
Dharman
  • 30,962
  • 25
  • 85
  • 135
Soroush Karimi
  • 343
  • 4
  • 10
1

The answer is probably simple. Just add import numpy.core.multiarray before the import cv2 statement. It worked fine for me.

DIVYA RATHOD
  • 229
  • 3
  • 15
1

My problem is solved using the old version of numpy. The solution is to use numpy 1.19.3.

pip install numpy==1.19.3

Credit: https://stackoverflow.com/a/64730012

Salma Elshahawy
  • 1,112
  • 2
  • 11
  • 21
0

Installing the previous version of NumPy, 1.19.3 should fix this.

python -m pip install numpy==1.19.3
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Prasoon Singh
  • 170
  • 1
  • 1
  • 9