在我使用 PyInstaller 将 PY 转换为 EXE 后,它抛出错误

After I convert PY to EXE using PyInstaller it throw error

在我将我的 PY 文件转换为 EXE 并 运行 它之后,我收到了这个错误:

Traceback (most recent call last):
  File "PrngCipher.py", line 2, in <module>
    from kivy.app import App
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "kivy\__init__.py", line 272, in <module>
  File "C:\Program Files\Python39\Lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgutil.py", line 71, in _pyi_pkgutil_iter_modules
    assert pkg_path.startswith(SYS_PREFIX)
TypeError: startswith first arg must be str or a tuple of str, not PureWindowsPath

我是 Python 和 PyInstaller 的新手,我不知道这个错误是什么,我按照许多教程将正确的依赖项挂钩到 .spec 文件中,但它仍然失败。 我的 .py、.kv 和 .spec 文件: https://drive.google.com/drive/folders/1F7I4xEphB3d2ErDPs7vGDpT7trTmHKgC?usp=sharing

错误告诉您您使用的 startsWith 参数类型有误,特别是,它期望 String 成为第一个参数,而您传递的却是 PureWindowsPath。我不是 Python 专家,但我相信您可以通过为要传递的变量调用 .__str__() 来解决它,因此它的值将表示为 String 时正在传递给 startsWith.

我做了更多研究并查看了错误消息的上面几行,我发现问题出在 PyInstaller pakage 的代码而不是我的代码,解决方法是将 PyInstaller 降级到 4.3 等待修复使用 python -m pip install PyInstaller==4.3 并且工作正常

这是 PyInstaller 4.4 中的一个回归错误:[GitHub]: pyinstaller/pyinstaller - TypeError: startswith first arg must be a str or tuple of str, not PurePosixPath.
It was already fixed (by [GitHub]: pyinstaller/pyinstaller - hooks: pkgutil rthook: declare SYS_PREFIX as function-local symbol),但修复将仅在下一个版本中可用,其中(考虑错误严重性)应该很快可用。

更进一步的方法:

  • PyInstaller 降级到 v4.3,没有这个错误:根据
    最简单的
  • 等到下一个 PyInstaller 版本 (4.5?) 可用,然后升级它。
    不可接受(最有可能)
  • 手动将修复应用到您的 PyInstaller 安装(因为它只是一个文件)。检查 修补 utrunner 部分)了解如何应用补丁(在 Win 上) .
    开发者方法