问题遇到的现象和发生背景
环境如下:
Python 3.10
Pyinstaller 5.1
numpy 1.22.4
opencv-python 4.6.0.66
问题现象:
1、使用pycharm运行或者调试一切正常,程序可以正常启动运行
2、使用pyinstaller打包时可以生成exe,但是运行exe时报错如下
问题相关代码,请勿粘贴截图
pyinstaller -D -n test abc.py --clean 报错内容如下,均相同
pyinstaller -F -n test abc.py --clean 报错内容如下,均相同
pyinstaller -D -n test abc.py --clean --hidden-import cv2 报错内容如下,均相同
运行结果及报错内容
File "cv2_init.py", line 181, in
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.
[13264] Failed to execute script 'onmyoji' due to unhandled exception!
我的解答思路和尝试过的方法
1、最终被打包的test文件夹里面有cv2文件夹,但是里面没有cv2.pyd,不知道是否有问题?
2、经过查看cv2的__init__.py代码,抛错的地方为if hasattr(sys, 'OpenCV_LOADER'):,hasattr(sys, 'OpenCV_LOADER')返回了false,如何才能让hasattr(sys, 'OpenCV_LOADER')返回true。