Program Tip

RuntimeError : 모듈이 API 버전 A에 대해 컴파일되었지만이 버전의 numpy는 9입니다.

programtip 2020. 10. 21. 21:22
반응형

RuntimeError : 모듈이 API 버전 A에 대해 컴파일되었지만이 버전의 numpy는 9입니다.


암호:

import numpy as np
import cv

콘솔:

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

시스템 정보 : OS X El Capitan, Macbook Air, 1.3GHz Intel Core i5, 8GB 1600 HMz DDR3

이미 numpy 업데이트를 시도했습니다. Spyder-Py2의 python2.7 폴더에 cv.py를 추가해야했습니다. 추가해야 할 다른 것이 있습니까?


numpy를 최신 버전으로 업그레이드

pip install numpy --upgrade

경로 확인

import numpy
print numpy.__path__

나를 위해 이것은 /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

다음 번에 numpy를 가져올 때 경로는 /Library/Python/2.7/site-packages/numpy/ init .pyc이고 모두 잘되었습니다.


이것은 나를 위해 일했습니다.

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

시스템 폴더에 이전 버전의 numpy가 설치된 Mac 기본값 (/ usr / bin / python)을 실행하고있을 가능성이 높습니다. Python을 opencv와 함께 사용하는 가장 쉬운 방법은 brew를 사용하여 python과 opencv를 모두 / usr / local에 설치하고 / usr / local / bin / python을 실행하는 것입니다.

brew install python
brew tap homebrew/science
brew install opencv

오늘 밤 같은 문제가 발생했습니다. 여러 개의 numpy 패키지가 설치된 경우 문제로 판명되었습니다. 에 이전 버전이 설치 /usr/lib/python2.7되었고에 올바른 버전이 설치되었습니다 /usr/local/lib/python2.7.

또한 PYTHONPATH=/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages. PYTHONPATH는 올바른 버전 이전에 numpy의 이전 버전을 찾았으므로 Python 인터프리터 내부에서 이전 버전의 numpy를 가져옵니다.

도움이 된 한 가지는 파이썬 세션을 열고 다음 코드를 실행하는 것입니다.

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

이는 Python이 사용중인 버전과 설치된 위치를 정확히 알려줍니다.

문제를 해결하기 위해 PYTHONPATH=/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages. 또한 Hitchiker의 Python 가이드 , 특히 "Lower level : virtualenv"섹션 사용하여 가상 Python 환경을 설정했습니다 . 처음에 가상 환경을 설정해야한다는 것을 알고 있지만 피곤하고 게으르다. 오, 교훈을 얻었습니다!

(최신 정보)

문서가 다시 이동되는 경우를 대비하여 관련 정보는 다음과 같습니다.

Python 가상 환경 만들기

pip를 통해 virtualenv를 설치합니다.

$ install virtualenv

설치 테스트 :

$ virtualenv --version

선택적으로 환경 변수 VIRTUALENVWRAPPER_PYTHON를 사용하여 가상 환경에서 사용하는 기본 Python 버전을 변경합니다 (예 : Python 3 사용).

$ export VIRTUALENVWRAPPER_PYTHON=$(which python3)

선택적으로 환경 변수 WORKON_HOME설정 하여 Python 가상 환경이 생성되는 기본 디렉토리를 변경합니다 (예 /opt/python_envs: 다음 을 사용) .

$ export WORKON_HOME=/opt/python_envs

프로젝트에 대한 가상 환경을 만듭니다.

$ cd my_project_folder
$ virtualenv my_virtual_env_name

방금 만든 가상 환경을 활성화합니다. 또한 설정했다고 가정합니다 WORKON_HOME=/opt/python_envs.

$ source $WORKON_HOME/my_virtual_env_name/bin/activate

다음 두 가지 방법 중 하나를 사용하여 프로젝트에 필요한 Python 패키지를 설치합니다.

방법 1- pip명령 줄에서 설치 :

$ pip install python_package_name1
$ pip install python_package_name2

방법 2- requests.txt파일을 사용하여 설치 :

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

선택 사항이지만 적극 권장되는 virtualenvwrapper. 가상 Python 환경에서보다 쉽게 ​​작업 할 수있는 유용한 명령이 포함되어 있습니다.

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

Windows에서는 다음을 사용 virtualenvwrapper하여 설치하십시오 .

$ pip install virtualenvwrapper-win

virtualenvwrapper의 기본 사용법 새 가상 환경을 만듭니다.

$ mkvirtualenv my_virtual_env_name

모든 가상 환경 나열 :

$ 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!


To solve the problem do following:

First uninstall numpy

sudo pip uninstall numpy

Install numpy with --no-cache-dir option

sudo pip install --no-cache-dir numpy

And to specify any specific version e.g. 1.14.2

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

I got the same issue with quaternion module. When updating modules with conda, the numpy version is not up^dated to the last one. If forcing update with pip command pip install --upgrade numpy + install quaternion module by pip install --user numpy numpy-quaternion, the issue is fixed. May be the issue is coming from the numpy version. Here the execution result:

Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import numpy as np
>>> print np.__version__
1.14.3
>>>

(base) C:\Users\jc>pip install --user numpy numpy-quaternion
Requirement already satisfied: numpy in d:\programdata\anaconda2\lib\site-packages (1.14.3)
Collecting numpy-quaternion
  Downloading https://files.pythonhosted.org/packages/3e/73/5720d1d0a95bc2d4af2f7326280172bd255db2e8e56f6fbe81933aa00006/numpy_quaternion-2018.5.10.13.50.12-cp27-cp27m-win_amd64.whl (49kB)
    100% |################################| 51kB 581kB/s
Installing collected packages: numpy-quaternion
Successfully installed numpy-quaternion-2018.5.10.13.50.12

(base) C:\Users\jc>python
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:34:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import numpy as np
>>> import quaternion
>>> 

For those using anaconda Python:

conda update anaconda

You might want to check your matplotlib version.

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

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


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

To check your path:

    echo $PYTHONPATH

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

    nano ~/.bashrc

Look for the line with export PYTHONPATH ...

After making changes, don't forget to

   source ~/.bashrc

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.

This works for me:

conda install anaconda

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

conda update numpy

I faced the same problem due to documentation inconsistencies. This page says the examples in the docs work best with python 3.x: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_intro/py_intro.html#intro , whereas this installation page has links to python 2.7, and older versions of numpy and matplotlib: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html

My setup was as such: I already had Python 3.6 and 3.5 installed, but since OpenCv-python docs said it works best with 2.7.x, I also installed that version. After I installed numpy (in Python27 directory, without pip but with the default extractor, since pip is not part of the default python 2.7 installation like it is in 3.6), I ran in this RuntimeError: module compiled against API version a but this version of numpy is error. I tried many different versions of both numpy and opencv, but to no avail. Lastly, I simply deleted numpy from python27 (just delete the folder in site-packages as well as any other remaining numpy-named files), and installed the latest versions of numpy, matplotlib, and opencv in the Python3.6 version using pip no problem. Been running opencv ever since.

Hope this saves somebody some time.


The below command worked for me :

conda install -c anaconda numpy

This works for me:

My pip is not work after upgrade, so the first thing I need to do is to fix it with

sudo gedit /usr/bin/pip

Change the line

from pip import main

to

from pip._internal import main

Then,

 sudo pip install -U numpy

This command solved my problem.

pip3 install --upgrade numpy 

참고URL : https://stackoverflow.com/questions/33859531/runtimeerror-module-compiled-against-api-version-a-but-this-version-of-numpy-is

반응형