Compile python object detection app relying on OpenCV with PyInstaller

3 days ago 11
ARTICLE AD BOX

I have written a python script that captures a video stream from a camera and performs object detection. I am using opencv for that.

opencv-python-headless 4.12.0.88 pypi_0 pypi pyinstaller 6.17.0 pypi_0 pypi pyinstaller-hooks-contrib 2025.10 pypi_0 pypi

This script works well on my machine when ran in the IDE. However, after compiling it with PyInstaller, I get DLL import errors like below:

Traceback (most recent call last): File "main.py", line 13, in <module> File "C:\Users\APU\AppData\Local\Temp\_MEI336202\cv2\__init__.py", line 181, in <module> bootstrap() File "C:\Users\APU\AppData\Local\Temp\_MEI336202\cv2\__init__.py", line 153, in bootstrap native_module = importlib.import_module("cv2") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "importlib\__init__.py", line 90, in import_module ImportError: DLL load failed while importing cv2: Le module spécifié est introuvable. [PYI-34956:ERROR] Failed to execute script 'main' due to unhandled exception!

After thorough search on the internet, it seems that PyInstaller and OpenCV integration is a well known issue but most of the threads talking about this problem are already 4 years old and suggest a downgrade to OpenCV 4.5.5.64.

What I tried without success:

using opencv-python-headless instead of opencv-python. downgrading to OpenCV 4.5.5.64 as suggested but I then got numpy multiarray import issues that I couldn't resolve even when trying different versions of numpy. I tried compiling my app as one file first without specifying hidden imports and additional binaries. I did not work. I tried to add cv2 as hidden import and it still didn't work. I tried getting rid of the --onefile flag and it prompted me the same error as shown above.

I didn't try to include DLLs via the --add-binary command as I am not sure which dlls I need to pass.

My questions are:

What is the recommended way to compile a python app using opencv as of now? Is it doable with Pyinstaller lastest and OpenCV latest or should I try another bundling tool?

Thanks a lot

Read Entire Article