3

I want to bundle some python code into a distributable application (.app file) but having trouble with getting opencv and PyQt5 into an application. Either, on the other hand, works fine.

I've stripped all my code and the problem persists. See the following code samples:

# Works absolutely fine
import cv2
print("Hi there opencv v{0} user!".format(cv2.__version__))

# Works absolutely fine
from PyQt5.QtCore import *
print("Qt version {0}".format(QT_VERSION_STR))

# Does not work, see below
import cv2
from PyQt5.QtCore import *
print("Qt version {0}".format(QT_VERSION_STR))
print("opencv version {0}".format(cv2.__version__))

And by "not work", I mean that it bundles without errors (python3.5 -m pyinstaller samplecode.py), but cannot be executed. Instead, the program quits and returns an error:

Traceback (most recent call last):
  File "/Users/*****/build/test.py", line 1, in <module>
    import cv2
  File "/Users/*****/build/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/cv2/__init__.py", line 9, in <module>
    from .cv2 import *
  File "/Users/*****/build/PyInstaller/loader/pyimod03_importers.py", line 714, in load_module
    module = loader.load_module(fullname)
ImportError: dlopen(/Users/*****/build/test/dist/test/cv2.cv2.so, 2): Symbol not found: __ZN10QByteArray11shared_nullE
  Referenced from: /Users/*****/build/test/dist/test/QtTest
  Expected in: /Users/*****/build/test/dist/test/QtCore
 in /Users/*****/build/test/dist/test/QtTest
[20049] Failed to execute script test

The stars just replace my personal and project folder names.

I tried manually copying the cv2.so file, renaming it to cv2.cv2.so and placing it in the folder. This produces the a slightly different error:

Traceback (most recent call last):
  File "/Users/*****/build/test.py", line 1, in <module>
    import cv2
  File "/Users/*****/build/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/cv2/__init__.py", line 9, in <module>
    from .cv2 import *
  File "/Users/*****/build/PyInstaller/loader/pyimod03_importers.py", line 714, in load_module
    module = loader.load_module(fullname)
ImportError: dlopen(/Users/*****/build/test/dist/test/cv2.cv2.so, 2): Library not loaded: @loader_path/.dylibs/QtGui
  Referenced from: /Users/*****/build/test/dist/test/cv2.cv2.so
  Reason: image not found

I've also tried cx_freeze and py2app but couldn't get these to work either. I got the furthest with pyinstaller and its development seems the most active so I prefer using this.

I'm running MacOS 10.12, and have reproduced the error on another clean-install MacBook running the same OS. I'm using Python 3.5.4, PyQt 5.9.1, OpenCV 3.3.0. For PyInstaller I've tried 3.2.1 and the most recent dev.

Can anyone help me understand and solve this error?

Neut
  • 76
  • 1
  • 5

3 Answers3

3

Using opencv-python-headless instead solved the problem for me, as mentionned in https://github.com/pyinstaller/pyinstaller/issues/3426.

Uninstall you opencv library, and then

pip install opencv-python-headless

https://pypi.org/project/opencv-python-headless/

Octave
  • 351
  • 2
  • 11
1

I encountered these same errors (using MacOS 10.11.6) when trying to execute my app, which I also built using pyinstaller. I was able to successfully resolve my issue by deprecating opencv and pyinstaller to the following versions:

pip install opencv-python == 3.1.0.4
pip install pyinstaller == 3.3.1

This is not a satisfying solution, but it may be worth trying if you are still struggling with this issue.

apogalacticon
  • 709
  • 1
  • 9
  • 19
0

I encountered a lot of problems like this, but after some investigating, I solved my problems by creating the virtual environment and install all the libraries I used in the project, then install the OpenCV and pyinstaller on that virtual environment like this: activate myEnvironment

> pip install opencv-python 
> pip install pyinstaller

and then used the pyinstaller in that virtual environment to create the exe file. it bundles all the dlls needed to software.

Ahad aghapour
  • 2,453
  • 1
  • 24
  • 33
  • Even i have a project that is having issue with OpenCV / Numpy being in same project. I did follow your instruction to install opencv / pyinstaller at the very end - still it fails and keeps giving in same error. – Blakdronzer Jan 21 '19 at 12:17
  • follow this link and check it: https://stackoverflow.com/a/48264244/1364213 – Ahad aghapour Jan 21 '19 at 16:52
  • well i did follow the pattern but it gives in the same result unfortunately :(. I am still stuck on with - ImportError: numpy.core.multiarray failed to import after i try running the final output. – Blakdronzer Jan 22 '19 at 06:04