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.学会冷静分析,学会根源排查。

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

相关文章
|
3月前
|
Python
python打包pyinstaller如何使用
解决打包时缺失`libpython3.so`的问题,需确保Python在编译时使用`--enable-shared`选项以支持共享库模式。之后,将生成的`libpython3.so`及`libpython3.9.so.1.0`复制到系统库目录`/usr/lib64`。参考链接提供详细步骤。
|
25天前
|
Linux 区块链 Python
Python实用记录(十三):python脚本打包exe文件并运行
这篇文章介绍了如何使用PyInstaller将Python脚本打包成可执行文件(exe),并提供了详细的步骤和注意事项。
46 1
Python实用记录(十三):python脚本打包exe文件并运行
|
2月前
|
存储 Shell 区块链
怎么把Python脚本打包成可执行程序?
该文档介绍了如何将Python脚本及其运行环境打包成EXE可执行文件,以便在不具备Python环境的计算机上运行。首先确保Python脚本能够正常运行,然后通过安装PyInstaller并使用`--onefile`参数将脚本打包成独立的EXE文件。此外,还提供了去除命令行窗口和指定可执行文件图标的详细方法。这些步骤帮助用户轻松地将Python程序分发给最终用户。
怎么把Python脚本打包成可执行程序?
|
24天前
|
XML JSON Ubuntu
Python实用记录(十五):PyQt/PySide6打包成exe,精简版(nuitka/pyinstaller/auto-py-to-exe)
本文介绍了使用Nuitka、PyInstaller和auto-py-to-exe三种工具将Python的PyQt/PySide6应用打包成exe文件的方法。提供了详细的安装步骤、打包命令和参数说明,适合新手学习和实践。
232 0
|
3月前
|
区块链 Python
最详细Python打包exe教程,并修改图标,只需30秒
最详细Python打包exe教程,并修改图标,只需30秒
88 4
最详细Python打包exe教程,并修改图标,只需30秒
|
3月前
|
Linux Python Windows
Python PDF文件转Word格式,只需要3秒(附打包)
Python PDF文件转Word格式,只需要3秒(附打包)
80 3
Python PDF文件转Word格式,只需要3秒(附打包)
|
2月前
|
安全 数据挖掘 Python
Python的打包工具(setup.py)实战篇
关于如何使用Python的setup.py工具打包Python项目的实战教程。
42 0
Python的打包工具(setup.py)实战篇
|
3月前
|
TensorFlow 算法框架/工具 C++
Python exe文件打包神器-Nuitka!
Python exe文件打包神器-Nuitka!
|
3月前
|
Unix API Apache
一站式解决 python打包代码,发布到pypi
本文是一份全面的Python项目打包并发布到PyPI的教程,涵盖了从PyPI账户注册、配置双因素认证、生成API Token,到准备代码、编写setup.py、选择LICENSE、构建包、上传包至PyPI,以及上传后的测试和库更新的详细步骤。
|
3月前
|
区块链 Python
Python脚本打包 exe,auto-py-to-exe来帮你!
Python脚本打包 exe,auto-py-to-exe来帮你!