Kivy/KivyMD 应用程序在 venv 环境中运行良好,但在 exe 文件中崩溃
Posted
技术标签:
【中文标题】Kivy/KivyMD 应用程序在 venv 环境中运行良好,但在 exe 文件中崩溃【英文标题】:Kivy/KivyMD app runs fine in venv environment but crashes in exe file 【发布时间】:2022-01-09 23:15:59 【问题描述】:我尝试使用我使用 Kivy/KivyMD 编写的代码制作一个 exe 文件。我能够成功生成exe文件。但是在我单击其中一个列表项后,应用程序崩溃并引发错误:
Traceback (most recent call last):
File "scanpy\_metadata.py", line 28, in <module>
File "pathlib.py", line 1221, in read_text
File "pathlib.py", line 1208, in open
File "pathlib.py", line 1063, in _opener
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\xie186\\AppData\\Local\\Temp\\_MEI82402\\pyproject.toml'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 178, in <module>
File "kivy\app.py", line 950, in run
File "kivy\base.py", line 582, in runTouchApp
File "kivy\base.py", line 347, in mainloop
File "kivy\base.py", line 391, in idle
File "kivy\base.py", line 342, in dispatch_input
File "kivy\base.py", line 308, in post_dispatch_input
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "kivymd\uix\list.py", line 854, in on_touch_up
File "kivymd\uix\behaviors\ripple_behavior.py", line 296, in on_touch_up
File "kivy\uix\behaviors\button.py", line 179, in on_touch_up
File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "main.py", line 118, in <lambda>
File "main.py", line 139, in set_example_screen
File "<string>", line 1, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "libs\baseclass\tabs.py", line 6, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "scanpy\__init__.py", line 3, in <module>
=======
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "scanpy\_metadata.py", line 38, in <module>
File "scanpy\_compat.py", line 25, in pkg_metadata
File "importlib_metadata\__init__.py", line 984, in metadata
File "importlib_metadata\__init__.py", line 561, in from_name
importlib_metadata.PackageNotFoundError: No package metadata was found for scanpy
[10680] Failed to execute script 'main' due to unhandled exception!
要重现错误,请使用此处的代码:
git clone https://github.com/xie186/pitaya.git
cd pitaya/
git checkout 067abaf4ead73862b2baa27909f6079f75fb2feb
pip install -r requirements.txt
python -m PyInstaller .\pyinstaller.spec
cd dist/
.\pitaya.exe
# Then click one of the list items.
根据错误,是因为这里的代码:https://github.com/theislab/scanpy/blob/16e62d1336a95966238113cdb5ab50f7d9f880ce/scanpy/_metadata.py#L28
Scany 是我的应用所依赖的包。它尝试在此处读取文件:https://github.com/theislab/scanpy/blob/master/pyproject.toml 但是运行exe文件时文件不可用。
如果我在 venv 环境中运行应用程序,它运行良好。任何帮助将不胜感激!
【问题讨论】:
【参考方案1】:解决方法: 由于这很可能是一个打包工件,我建议通过更改“scanpy\_metadata.py”文件来绕过它。具体的,在异常语句之后写
except (ImportError, LookupError, FileNotFoundError):
__version__ = '0.0.0' # or the real version number
__author__ = 'Theis lab'
__email__ = 'f.alex.wolf@gmx.de, philipp.angerer@helmholtz-muenchen.de'
这样您的代码就不会尝试(并且失败)找到“pyproject.toml”文件,也不会要求 importlib_metadata 为 scanpy 定位包元数据(并且也会失败)
【讨论】:
以上是关于Kivy/KivyMD 应用程序在 venv 环境中运行良好,但在 exe 文件中崩溃的主要内容,如果未能解决你的问题,请参考以下文章
第一个 Python/Kivy/KivyMD 应用程序。无法将文本字段中的数据保存到变量 + 数据绑定到 MDList
相当于 Kivy/KivyMD 中的 `wx.CallLater()` 或者如何处理 `time.sleep()` 冻结 GUI?