python之pyinstaller打包问题系列(二)

简介: 学会冷静分析,学会根源排查。

python之pyinstaller打包问题系列(二)

之前也写过一篇pyinstaller的文章,链接如下:

python打包exe——pyinstaller遇到的那些坑及解决办法

工具需求

根据项目需求,做一个ios连点器

1.使用airtest操作ios设备

2.使用坐标进行点击(可自行输入)

3.使用线程分布点击加快速度(速度可自行调节)

代码完事后都能够成功打包

image-20221011103046595.png

但是遇到了如下两个问题

问题一

运行exe报错:

ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV in

报错截图

image-20221011103145755.png

整体报错内容

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

报错截图

image-20221011104059388.png

整体报错内容

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

运行成功

image-20221011102011345.png

总结

1.遇到问题不要慌张,代码在编辑器里面运行正常,不代表打包后运行正常。

2.多考虑兼容性问题,代码本身可以运行,说明不是代码自身的问题。

3.有报错提示,根据报错提示一步步分析,查阅资料时,需要准确判断对方遇到的问题是否和我们一致,不能盲目抄解决方案。

4.学会冷静分析,学会根源排查。

更多内容欢迎关注我的公众号:梦无矶的测试开发之路

相关文章
|
4天前
|
缓存 前端开发 测试技术
(译)Python 官方团队在打包项目中踩过的坑
(译)Python 官方团队在打包项目中踩过的坑
34 2
|
4天前
|
Python Windows
win64系统安装32位的python解释器和打包成exe程序
本文说明了在win64系统环境下,如何安装32位的python解释器,同时对32位虚拟环境下运行的python程序进行打包,将其打包成可执行的exe程序,以图文相结合的方式记录了操作步骤,供大家参考。
283 0
|
4天前
|
Linux iOS开发 MacOS
pyinstaller---Python代码的打包神器,一键将python代码打包成exe可执行文件
pyinstaller---Python代码的打包神器,一键将python代码打包成exe可执行文件
|
4天前
|
测试技术 持续交付 项目管理
hatch,现代化的 Python 项目管理和打包工具!
hatch,现代化的 Python 项目管理和打包工具!
26 2
|
4天前
|
开发者 索引 Python
Python应用发布常见打包
【4月更文挑战第5天】Python应用发布常见打包
26 1
|
4天前
|
Python
python脚本打包成可执行文件(pyinstaller)
python脚本打包成可执行文件(pyinstaller)
|
4天前
|
存储 UED 开发者
Python语言的软件打包及发布
Python语言的软件打包及发布
|
4天前
|
Linux 数据库连接 数据库
Python如何将项目直接打包为一键整合包
Python如何将项目直接打包为一键整合包
59 0
|
4天前
|
存储 前端开发 API
Python 打包——过去、现在与未来
Python 打包——过去、现在与未来
26 0
|
4天前
|
存储 编译器 开发者
Python 打包的现状:包的三种类型
Python 打包的现状:包的三种类型
26 1