Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation." with Pyinstaller #7128

Closed
Josh596 opened this issue Oct 4, 2022 · 4 comments · Fixed by #7180
Labels

Comments

@Josh596
Copy link

Josh596 commented Oct 4, 2022

Environment:

Mac M1 - macOS Monterey

PyInstaller: 5.4.1

Python: 3.8.10

opencv-contrib-python==4.6.0.66
opencv-python==4.6.0.66

Script
pyinstaller -n "App" --windowed App.py

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:

Traceback (most recent call last):
  File "App.py", line 6, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 499, in exec_module
  File "main.py", line 28, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 499, in exec_module
  File "image_manager.py", line 6, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 499, in exec_module
  File "workers/image_worker.py", line 6, in <module>
  File "PyInstaller/loader/pyimod02_importers.py", line 499, in exec_module
  File "modules/apply_effects.py", line 3, in <module>
  File "/Users/Josh/Desktop/Work-Stuff/App-Offline/dist/App.app/Contents/MacOS/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/Users/Josh/Desktop/Work-Stuff/App-Offline/dist/App.app/Contents/MacOS/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 "/Users/Josh/Desktop/Work-Stuff/App-Offline/dist/App.app/Contents/MacOS/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/Users/Josh/Desktop/Work-Stuff/App-Offline/dist/App.app/Contents/MacOS/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.

I fixed this by replacing the cv2 folder in Contents/Resources with the cv2 folder in site-packages.

Notes:
I ran the diff -r .venv/lib/python3.8/site-packages/cv2 dist/App.app/Contents/Resources/cv2 >> cv2-diff.txt to see the difference between the two directories, and this was the relevant output.

Only in .venv/lib/python3.8/site-packages/cv2: .dylibs
Only in .venv/lib/python3.8/site-packages/cv2: cv2.abi3.so

Update:
I don't need to copy the whole cv2 folder from site-packages, I only need to copy the .dylibs directory and the cv2.abi3.so file from site-packages/cv2

@Josh596 Josh596 added the triage Please triage and relabel this issue label Oct 4, 2022
@rokm
Copy link
Member

rokm commented Oct 4, 2022

What version of pyinstaller-hooks-contrib do you have installed?

@rokm
Copy link
Member

rokm commented Oct 4, 2022

Nevermind, I've reproduced the problem, which is applicable only to .app bundles.

What happens is that we move the collected cv2 .py files from Contents/MacOS/cv2 to Contents/Resources/cv2 and symlink them back. But we don't do the same with the extension, and therefore the cv2 loader code fails to set the correct path to the extension (because it resolves the actual paths to .py files). Adding cv2 as an exemption similar to what we already do for PyQt/PySide directories (here) should also fix the problem - although god knows what notarization has to say about it...

@rokm rokm added bug and removed triage Please triage and relabel this issue labels Oct 4, 2022
@Josh596
Copy link
Author

Josh596 commented Oct 6, 2022

Hey @rokm, I did as you said and on my local version of PyInstaller edited the osx.py and added cv2 to the set of exceptions as it was done (here), and it worked. Should I create a PR?

@rokm
Copy link
Member

rokm commented Oct 6, 2022

I think instead of exempting cv2 from relocation, we will need to exempt all .py files, because this will be a general problem with packages where we collect sources (perhaps moreso packages where we collect only sources, although that's only cv2 at the moment). I'll create a PR once I do some testing.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants