Python+OpenCV+py2app: numpy.core.multiarray failed to import

不羁的心 提交于 2019-12-10 20:16:00

问题


Environment: mac os x 10.7.5, xcode 4.2.1, python 2.7.5, opencv 2.4.7, py2app 0.7.3

I am trying to package a simple opencv based python script using py2app but the built app crashes with an error that says ImportError: numpy.core.multiarray failed to import

here is the python script called demoApp.py

import cv2
capture = cv2.VideoCapture(0)
winName = 'eyeDetection'
cv2.namedWindow(winName)

# Press esc key to exit
keyPressed = -1
while(keyPressed != 27): # ord('esc') is 27
    unused_retval, img0 = capture.read()
    img1 = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)

    cv2.imshow(winName, img1) 
    keyPressed = cv2.waitKey(1)
cv2.destroyAllWindows()

demoApp.py runs as expected when launched from eclipse+pydev IDE.

I create the setup.py file:

py2applet --make-setup demoApp.py

which has the following content:

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup

APP = ['demoApp.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

I then build the app:

python setup.py py2app

Running the app causes it to crash with the above mentioned ImportError.

I deleted the build and dist folders and tried building the app in "alias" mode:

python setup.py py2app -A

Then when I run the app it runs as expected. So I don't understand why the standalone app won't work when built for distribution.

Please help me figure out how to deal with this issue. Also, the demoApp.app is 50Mb, how can I reduce its size?


回答1:


though I haven't found a proper solution and this problem might be due to 64bit python27 and 32bit numpy computability issues a quick work around was to import numpy in the demoApp.py script before importing cv2.

and after using PyInstaller instead of py2app the demoApp.app file is now 6Mb instead of 50Mb :D

though the app generated via PyInstaller gave the same problem without the above workaround.



来源:https://stackoverflow.com/questions/20242421/pythonopencvpy2app-numpy-core-multiarray-failed-to-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!