Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to bundle OpenCV and PyQt5

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?

like image 398
Neut Avatar asked Dec 04 '25 07:12

Neut


1 Answers

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/

like image 179
Octave Avatar answered Dec 05 '25 21:12

Octave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!