"Recursion is detected during loading of "cv2" binary extensions" #7493
-
Context / environment
End goalHave a single file Ubuntu executable that runs some primitive video analytics (object detection) in real time on a GStreamer stream from an IP camera. What I am trying to doI need OpenCV with support of GStreamer so I build OpenCV from source rather than pip install it. Specifically I follow procedure outlined here , i.e. run the following commands: The file I am trying to package is After what seems to be a successful build, I try to run the resulting app: but am getting an "ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation." What I am unable to resolveI have tried to downgrade only OpenCV (every time building it from source) to 4.6.0, 4.5.5, 4.5.4 while keeping latest PyInstaller version but encountered the same error even though the vast majority of people encountering this issue resolved it by downgrading OpenCV to one those versions (see references). Also tried to downgrade only PyInstaller with pyinstaller-hooks-contrib to 5.8.0, 5.7.0 and 2022.15, 2022.14 respectively while keeping OpenCV at the latest 4.7.0 but the error stays the same. I have also tried to modify the PyInstaller command to use Note that running the original In addition, following recommendations from "Make sure everything is packaged" I looked through PYZ-00.pyz in References of what seems to be the same issue:
The ask
AttachmentsOutput of
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
What are the contents of |
Beta Was this translation helpful? Give feedback.
-
|
The issue still occured for me even after the fix from @rokm . Here is my solution, I hope this helps someone. I'm not an expert on any of these tools or on doing builds, so please correct me if I am wrong anywhere.
The solution to this issue sadly did not fix my problem. TLDR: The problem is happening because pyinstaller doesn't know how to import opencv for certain opencv versions. The fix is to create a runtime hook that appends to sys.path the location where opencv's dynamic library is. Long: It does this by deleting itself from sys.modules and then reimporting itself immediately (here). The current running cv2 module HOPES (im' sure it does more than hope, but since it broke for this use case, I am saying hope) that python's import mechanism is able to find the dynamic library before it finds the python import logic in init.py because if the init.py is found first, there will be infinite import recursion (hence our lovely error). The cv2 python module init.py script that I am working with (which is [here](https://github.com/opencv/opencv/blob/ba483dfbb8d9c1a5b9c25fcb444e76ba9df74f09/modules/python/package/cv2/__init__.py at the time of writing), puts the dynamic library in Fix: I hope this helps. I am sorry in advance for any spelling or grammar mistakes! I didn't check too hard. |
Beta Was this translation helpful? Give feedback.
If
config-3.11.pycontains:(which it should by default, unless you override
OPENCV_PYTHON_INSTALL_PATH, which you do not seem to be doing), then it is likely that the only problem is that the extension is not collected because it is located inpython-3.11subdirectory, wheras the hook (which was written for PyPI wheels) expects it to be present in the main directory (hence you get the warning about hidden importcv2.cv2not being found).Can you try modifying your local copy of the hook (
/usr/local/lib/python3.10/dist-packages/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-cv2.py) and add…