Python 运行时错误:针对 API 版本 a 编译的模块,但此版本的 numpy 是 9

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33859531/
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 14:07:10  来源:igfitidea点击:

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

pythonpython-2.7opencvnumpy

提问by Isaiah Nields

Code:

代码:

import numpy as np
import cv

Console:

安慰:

>>> runfile('/Users/isaiahnields/.spyder2/temp.py', wdir='/Users/isaiahnields/.spyder2')
RuntimeError: module compiled against API version a but this version of numpy is 9
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/Users/isaiahnields/.spyder2/temp.py", line 9, in <module>
import cv
File "/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/cv.py", line 1, in <module>
from cv2.cv import *
ImportError: numpy.core.multiarray failed to import
>>> 

System Info: OS X El Capitan, Macbook Air, 1.3 GHz Intel Core i5, 8 GB 1600 HMz DDR3

系统信息:OS X El Capitan、Macbook Air、1.3 GHz Intel Core i5、8 GB 1600 HMz DDR3

I have already attempted updating numpy. I had to add cv.py to the python2.7 folder in Spyder-Py2 is there something else I need to add?

我已经尝试更新 numpy。我必须将 cv.py 添加到 Spyder-Py2 中的 python2.7 文件夹中,还有什么我需要添加的吗?

回答by Pat Niemeyer

You are likely running the Mac default (/usr/bin/python) which has an older version of numpy installed in the system folders. The easiest way to get python working with opencv is to use brew to install both python and opencv into /usr/local and run the /usr/local/bin/python.

您可能正在运行 Mac 默认 (/usr/bin/python),它在系统文件夹中安装了旧版本的 numpy。让python与opencv一起工作的最简单方法是使用brew将python和opencv安装到/usr/local并运行/usr/local/bin/python。

brew install python
brew tap homebrew/science
brew install opencv

回答by JnBrymn

Check the path

检查路径

import numpy
print numpy.__path__

For me this was /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy So I moved it to a temporary place

对我来说这是 /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy 所以我把它移到了一个临时的地方

sudo mv /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy \
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy_old

and then the next time I imported numpy the path was /Library/Python/2.7/site-packages/numpy/init.pyc and all was well.

然后下次我导入 numpy 时,路径是 /Library/Python/2.7/site-packages/numpy/ init.pyc 一切都很好。

回答by Joshua Owoyemi

upgrade numpy to the latest version

将 numpy 升级到最新版本

pip install numpy --upgrade

回答by YakovK

For those using anaconda Python:

对于那些使用 anaconda Python 的人:

conda update anaconda

回答by MikeyE

I ran into the same issue tonight. It turned out to be a problem where I had multiple numpy packages installed. An older version was installed in /usr/lib/python2.7and the correct version was installed in /usr/local/lib/python2.7.

我今晚遇到了同样的问题。结果证明是我安装了多个 numpy 包的问题。中安装了较旧的版本,/usr/lib/python2.7并在 中安装了正确的版本/usr/local/lib/python2.7

Additionally, I had PYTHONPATH=/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages. PYTHONPATH was finding the older version of numpy before the correct version, so when inside the Python interpreter, it would import the older version of numpy.

此外,我有PYTHONPATH=/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages. PYTHONPATH 在找到正确版本之前找到旧版本的 numpy,因此在 Python 解释器中时,它会导入旧版本的 numpy。

One thing which helped was opening a python session an executing the following code:

有帮助的一件事是打开一个 python 会话并执行以下代码:

import numpy as np 
print np.__version__ 
print np.__path__

That should tell you exactly which version Python is using, and where it's installed.

这应该会告诉您 Python 正在使用哪个版本,以及它的安装位置。

To fix the issue, I changed PYTHONPATH=/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages. And I also setup a virtual Python environment using the Hitchiker's Guide to Python, specifically the section titled "Lower level: virtualenv" . I know I should have setup a virtual environment in the first place, but I was tired and being lazy. Oh well, lesson learned!

为了解决这个问题,我改变了PYTHONPATH=/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages. 我还使用Hitchiker's Guide to Python设置了一个虚拟 Python 环境,特别是标题为 "Lower level: virtualenv" 的部分。我知道我应该首先设置一个虚拟环境,但是我很累而且很懒。呵呵,吸取教训了!

(Update)

(更新)

Just in case the docs are moved again, here are the relevant bits on...

以防万一文档再次移动,这里是相关的位...

Creating a Python Virtual Environment

创建 Python 虚拟环境

Install virtualenv via pip:

通过 pip 安装 virtualenv:

$ install virtualenv

Test the installation:

测试安装:

$ virtualenv --version

Optionally, et the environment variable VIRTUALENVWRAPPER_PYTHONto change the default version of python used by virtual environments, for example to use Python 3:

或者,设置环境变量VIRTUALENVWRAPPER_PYTHON以更改虚拟环境使用的默认 Python 版本,例如使用 Python 3:

$ export VIRTUALENVWRAPPER_PYTHON=$(which python3)

Optionally, set the environment variable WORKON_HOMEto change the default directory your Python virtual environments are created in, for example to use /opt/python_envs:

或者,设置环境变量WORKON_HOME以更改创建 Python 虚拟环境的默认目录,例如使用/opt/python_envs

$ export WORKON_HOME=/opt/python_envs

Create a virtual environment for a project:

为项目创建虚拟环境:

$ cd my_project_folder
$ virtualenv my_virtual_env_name

Activate the virtual environment, you just created. Assuming you also set WORKON_HOME=/opt/python_envs:

激活您刚刚创建的虚拟环境。假设您还设置了WORKON_HOME=/opt/python_envs

$ source $WORKON_HOME/my_virtual_env_name/bin/activate

Install whatever Python packages your project requires, using either of the following two methods.

使用以下两种方法之一安装项目所需的任何 Python 包。

Method 1 - Install using pipfrom command line:

方法 1 -pip从命令行安装:

$ pip install python_package_name1
$ pip install python_package_name2

Method 2 - Install using a requests.txtfile:

方法 2 - 使用requests.txt文件安装:

$ echo "python_package_name1" >> requests.txt
$ echo "python_package_name2" >> requests.txt
$ pip install -r ./requests.txt

Optionally, but highly recommended, install virtualenvwrapper. It contains useful commands to make working with virtual Python environments easier:

可选,但强烈推荐,安装virtualenvwrapper. 它包含有用的命令,可以更轻松地使用虚拟 Python 环境:

$ pip install virtualenvwrapper
$ source /usr/local/bin/virtualenvwrapper.sh

On Windows, install virtualenvwrapperusing:

在 Windows 上,安装virtualenvwrapper使用:

$ pip install virtualenvwrapper-win

Basic usage of virtualenvwrapperCreate a new virtual environment:

virtualenvwrapper 的基本用法创建一个新的虚拟环境:

$ mkvirtualenv my_virtual_env_name

List all virtual environments:

列出所有虚拟环境:

$ lsvirtualenv

Activate a virtual environment:

激活虚拟环境:

$ workon my_virtual_env_name

Delete a virtual environment (caution! this is irreversible!):

删除虚拟环境(注意!这是不可逆的!):

$ rmvirtualenv my_virtual_env_name

I hope this help!

我希望这有帮助!

回答by hlin

You might want to check your matplotlib version.

您可能想检查您的 matplotlib 版本。

Somehow I installed a dev version of matplotlib which caused the issue. A downgrade to stable release fixed it.

不知何故,我安装了导致问题的 matplotlib 的开发版本。降级到稳定版本修复了它。

One can also can try python -v -c 'import YOUR_PACKAGE' 2>&1 | lessto see where the issue occurred and if the lines above error can give you some hints.

您还可以尝试python -v -c 'import YOUR_PACKAGE' 2>&1 | less查看问题发生的位置,以及上面的错误行是否可以给您一些提示。

回答by gtcoder

You may also want to check your $PYTHONPATH. I had changed mine in ~/.bashrcin order to get another package to work.

您可能还想检查您的$PYTHONPATH. ~/.bashrc为了让另一个包工作,我已经改变了我的。

To check your path:

要检查您的路径:

    echo $PYTHONPATH

To change your path (I use nano but you could edit another way)

改变你的路径(我使用 nano 但你可以用另一种方式编辑)

    nano ~/.bashrc

Look for the line with export PYTHONPATH...

寻找带有export PYTHONPATH...的行

After making changes, don't forget to

更改后,不要忘记

   source ~/.bashrc

回答by a20

This worked for me:

这对我有用:

sudo pip install numpy --upgrade --ignore-installed

回答by Luce Philibert

I had the same error when trying to launch spyder. "RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa". This error appeared once I modified the numpy version of my machine by mistake (I thought I was in a venv). If your are using spyder installed with conda, my advice is to only use conda to manage package.

我在尝试启动 spyder 时遇到了同样的错误。“运行时错误:针对 API 版本 0xb 编译的模块,但此版本的 numpy 是 0xa”。一旦我错误地修改了我机器的 numpy 版本(我以为我在 venv 中),就会出现这个错误。如果您使用的是与 conda 一起安装的 spyder,我的建议是仅使用 conda 来管理包。

This works for me:

这对我有用:

conda install anaconda

(I had conda but no anaconda on my machine) then:

(我的机器上有 conda 但没有 anaconda)然后:

conda update numpy

回答by GauravLuthra

To solve the problem do following:

要解决问题,请执行以下操作:

First uninstall numpy

首先卸载numpy

sudo pip uninstall numpy

Install numpy with--no-cache-diroption

使用--no-cache-dir选项安装 numpy

sudo pip install --no-cache-dir numpy

And to specify any specific version e.g. 1.14.2

并指定任何特定版本,例如 1.14.2

sudo pip install --no-cache-dir numpy==1.14.2