samkhan13
samkhan13

Reputation: 3385

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

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?

Upvotes: 3

Views: 3501

Answers (1)

samkhan13
samkhan13

Reputation: 3385

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.

Upvotes: 2

Related Questions