python之pyinstaller打包问题系列(二)
之前也写过一篇pyinstaller的文章,链接如下:
python打包exe——pyinstaller遇到的那些坑及解决办法
工具需求
根据项目需求,做一个ios连点器
1.使用airtest操作ios设备
2.使用坐标进行点击(可自行输入)
3.使用线程分布点击加快速度(速度可自行调节)
代码完事后都能够成功打包
但是遇到了如下两个问题
问题一
运行exe报错:
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV in
报错截图
整体报错内容
OpenCV bindings requires "numpy" package.
Install it via command:
pip install numpy
Traceback (most recent call last):
File "numpy\core\__init__.py", line 23, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "numpy\core\multiarray.py", line 10, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "numpy\core\overrides.py", line 6, in <module>
ImportError: DLL load failed while importing _multiarray_umath: 找不到指定的模块。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "IOSClick_1.py", line 2, in <module>
from airtest.core.api import *
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "airtest\core\api.py", line 10, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "airtest\core\cv.py", line 13, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "airtest\aircv\__init__.py", line 1, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "airtest\aircv\aircv.py", line 5, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "cv2\__init__.py", line 11, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "numpy\__init__.py", line 140, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "numpy\core\__init__.py", line 49, in <module>
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.8 from "D:\Y_PythonProject\IOSClickTool\IOSClick_1.exe"
* The NumPy version is: "1.23.3"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: DLL load failed while importing _multiarray_umath: 找不到指定的模块。
[11604] Failed to execute script 'IOSClick_1' due to unhandled exception!
粗略分析
1.最开始处OpenCV bindings requires "numpy" package.
应该是opencv模块和numpy发生冲突
2.检查airtest版本为1.2.6
, numpy版本为1.23.3
3.此处解决方案猜测,将numpy版本降级,我降级为了1.19.3
,pip install numpy==1.19.3
使用这种解决方案之后,重新打包,打包成功,运行报了第二个错误,如下:
问题二
运行exe报错:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! Importing the numpy C-extensions failed. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed. We have compiled some common reasons
报错截图
整体报错内容
Traceback (most recent call last):
File "IOSClick_1.py", line 2, in <module>
from airtest.core.api import *
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "airtest\core\api.py", line 10, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "airtest\core\cv.py", line 13, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "airtest\aircv\__init__.py", line 1, in <module>
'''
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "airtest\aircv\aircv.py", line 5, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "cv2\__init__.py", line 181, in <module>
for submodule in __collect_extra_submodules(DEBUG):
File "cv2\__init__.py", line 153, in bootstrap
# amending of LD_LIBRARY_PATH works for sub-processes only
File "importlib\__init__.py", line 127, in import_module
applySysPathWorkaround = False
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "cv2\__init__.py", line 181, in <module>
for submodule in __collect_extra_submodules(DEBUG):
File "cv2\__init__.py", line 76, in bootstrap
import sys
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.
[26304] Failed to execute script 'IOSClick_1' due to unhandled exception!
粗略分析
1.我们看到倒数几行的报错提示,Check OpenCV installation.
,检查是否下载opencv
2.发现只有一个opencv-contrib-python==4.6.0.66
,确实没有opencv-python
3.下载pip install opencv-python
4.发现依旧报错,回想问题一,是否是版本兼容问题,查阅资料后,验证猜想正确,pyinstaller和cv2版本存在兼容问题
5.将版本降级pip install opencv-python==4.5.3.56
6.再次打包,成功运行
解决方案
问题一解决方案:降numpy版本
pip install numpy==1.19.3
问题二解决方案:下载opencv为如下版本,如果下载了最新的则降opencv版本
pip install opencv-python==4.5.3.56
运行成功
总结
1.遇到问题不要慌张,代码在编辑器里面运行正常,不代表打包后运行正常。
2.多考虑兼容性问题,代码本身可以运行,说明不是代码自身的问题。
3.有报错提示,根据报错提示一步步分析,查阅资料时,需要准确判断对方遇到的问题是否和我们一致,不能盲目抄解决方案。
4.学会冷静分析,学会根源排查。
更多内容欢迎关注我的公众号:梦无矶的测试开发之路