opencv-python: ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.
Seeing a similar issue to https://github.com/opencv/opencv-python/issues/432 with opencv-python==4.6.0.66 packaged using pyinstaller==4.4. It works fine when running from source; the issue only occurs when packaged. opencv-python==4.5.5.64 works fine.
My Env: Ubuntu 16 Python3.8 opencv-python==4.6.0.66 pyinstaller==4.4
Traceback (most recent call last):
File "ftf_app_bambam.py", line 2, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "taskmaster/app_manager.py", line 30, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "hardware/ximea_camera/setup_tools/filter_tuning.py", line 9, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "cv2/__init__.py", line 181, in <module>
bootstrap()
File "cv2/__init__.py", line 153, in bootstrap
native_module = importlib.import_module("cv2")
File "importlib/__init__.py", line 127, in import_module
applySysPathWorkaround = True
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "cv2/__init__.py", line 181, in <module>
bootstrap()
File "cv2/__init__.py", line 76, in bootstrap
raise ImportError('ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.')
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.
[11606] Failed to execute script 'ftf_app_bambam' due to unhandled exception!
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 5
- Comments: 23 (3 by maintainers)
Same here. I also had to revert to 4.5.5.64
Same problem on Windows 11 with Python 3.10.2, numpy==1.23.1, pyinstaller==5.2 and opencv-python==4.6.0.66
['C:\\Users\\a13\\Desktop\\build\\dist\\timelapse\\base_library.zip', 'C:\\Users\\a13\\Desktop\\build\\dist\\timelapse\\cv2', 'C:\\Users\\a13\\Desktop\\build\\dist\\timelapse\\lib-dynload', 'C:\\Users\\a13\\Desktop\\build\\dist\\timelapse'] Traceback (most recent call last): File "main.py", line 6, in <module> import cv2 File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module File "cv2\__init__.py", line 181, in <module> bootstrap() File "cv2\__init__.py", line 153, in bootstrap native_module = importlib.import_module("cv2") File "importlib\__init__.py", line 126, in import_module if sys.path[0] == BASE_DIR or os.path.realpath(sys.path[0]) == BASE_DIR: File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module File "cv2\__init__.py", line 181, in <module> bootstrap() File "cv2\__init__.py", line 76, in bootstrap raise ImportError('ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.') ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.
With opencv-python==4.5.5.64 works like a charm.
I use Windows 10, python 3.8, newest version of opencv-python. When I try to pack a exe with pyinstaller, I have the exact same problem. opencv-python==4.5.5.64 works fine.
I compared two version of init.py Maybe the key is in line 162: 4.5.5.64: native_module = importlib.import_module(“cv2.cv2”) 4.6.0.66: native_module = importlib.import_module(“cv2”)
For python 3.9 need opencv 4.5.5.64 version.
Work for me ! Thx
I’m not sure if the cause of my issue is the same as others as I’m on a different OS/environment and stuff but hopefully this helps someone as I was able to resolve it.
Environment:
Here was the output I was getting while trying to run my program after bundling in PyInstaller.
Output
I had to try and find a way around this without downgrading OpenCV because I unfortunately was using modules that had very specific dependencies.
In order to reduce the number of variables that could be causing the issue, I created the following test script that only imports
sys
andcv2
:opencv-test.py
When running the script with
python opencv-test.py
, it finished successfully without any issues. I noticed that I got the following PYTHON_EXTENSIONS_PATHS, which thanks to some of the discussion in this issue thread, clued me in that it might be part of the issue.When I bundled it with PyInstaller, and tried to run it by invoking
./dist/opencv-test.app/Contents/MacOS/opencv-test
, it produced the “recursion” error again. Before the exception got raised, it did print out some of the directories and indicated that it was looking for the following PYTHON_EXTENSIONS_PATHS:The recursion error got triggered after printing the
Relink everything from native cv2 module to cv2 package
debug message of the OpenCV loader in./cv2/__init__.py
.Output
Thankfully I’m on a Mac, which lets devs actually dig into the
.app
file even if you’re using--onefile
. When I went into the bundled./dist/opencv-test.app
, I noticed that the folder./dist/opencv-test.app/Contents/MacOS/cv2/python-3.8
was missing – it seemed PyInstaller was not including it. (Recall: this was the PYTHON_EXTENSIONS_PATHS given above when OpenCV was being bundled by PyInstaller.)When I looked inside the anaconda version in
~/anaconda3/envs/MegadetectorApp-m1/lib/python3.8/site-packages/cv2/python-3.8
, there was a.so
file namedcv2.cpython-38-darwin.so
. I copied that directly into the.app
, into./dist/opencv-test.app/Contents/MacOS/cv2/python-3.8
, which resolved the “recursion” error. Why this is being raised as a “recursion” error isn’t exactly clear to me, but then I got errors that some.dylibs
were missing.Output
This made me suspect that PyInstaller was not able to see all of the libraries that it needs to bundle without some help.
I got some inspiration from https://github.com/pyinstaller/pyinstaller/issues/6624 and managed to get this to work by adding the following the PyInstaller
.spec
file.opencv-test.spec
Of course on different systems you may need to change a few things, like
python3.8
andpython-3.8
to whatever version of Python you’re using, change thecv2.cpython-38-darwin.so
file to whatever one was included for your system, and to change thecv2_path
to wherever yourcv2
lives.I’m hoping I didn’t forget anything but that was what worked for me.
So I suspect that at least one of the causes of this issue are that static and dynamic library files are not getting included by PyInstaller in some environments. Perhaps that means some PyInstaller hooks for
cv2
need to be updated. Also, I’m not sure I understand enough about how the OpenCV loader and PyInstaller interact but it would be worth investigating why missing libraries trigger a recursive loading issue.Same error.
opencv-python==4.5.5.64
works@asenyaev You didn’t show the result of running the executable? The exception doesn’t happen during the pyInstaller creation step, but only when the resulting executable is run, e.g.
./dist/test
(or./dist/test/test
when--onefile
isn’t used). I hope that’s the difference.@asmorkalov I just quickly tried the code change used in the MR fix for #689 (i.e. https://github.com/opencv/opencv/pull/22269/files), but it didn’t make a difference to this issue on my local system. I might’ve made some mistake, but I suspect the iPython recursion issue might be different from this pyInstaller issue, and might not be fixed by that MR.
However, it looks like pyInstaller is aware of the issue, and they might have a fix on their side in an upcoming version: https://github.com/pyinstaller/pyinstaller/issues/6964#issuecomment-1193333632
Environment:
Windows 10
PyInstaller: 5.2
Python: 3.10.5
opencv-python 4.6.0.66
Problem:
I am trying to compile my Python code with PyInstaller. The exe is created and when I execute it, I get this error message:
What did I try:
I think I tried it all:
I installed the previous version of OpenCV:
pip install opencv-python==4.5.3.56
It didn’t work
I installed the latest version:
pip install --upgrade opencv-python pip install --upgrade pyinstaller
It didn’t work
I tried to force the reinstallation:
pip3 install opencv-python --upgrade --force-reinstall
It didn’t work
I added the path of cv2:
pyinstaller --onefile -F --uac-admin --icon=“icon_PhoneBot_256.ico” --clean --noconsole --collect-data pyshadow --paths=“C:\Users\gauth\AppData\Local\Programs\Python\Python310\Lib\site-packages\cv2” Project.py
It didn’t work
Here are the logs of PyInstaller if it can help:
Is there anyone who has any idea how to fix this issue?
I have the same problem however it only appears if I run my python project in trace mode python 3.8 opencv-contrib-python 4.6.0.66
python3 -u -m trace -c run.py
otherwise it works normally