不能运行 pyinstaller打包的.exe

Can not run .exe packed by pyinstaller

My source code

我在我的项目中使用 pyfiglet,在编译 .exe 时 returns

Traceback (most recent call last):
  File "pkg_resources\__init__.py", line 358, in get_provider
KeyError: 'pyfiglet.fonts'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "Dragon_Game.py", line 5, in <module>
  File "pyfiglet\__init__.py", line 65, in figlet_format
  File "pyfiglet\__init__.py", line 820, in __init__
  File "pyfiglet\__init__.py", line 827, in setFont
  File "pyfiglet\__init__.py", line 126, in __init__
  File "pyfiglet\__init__.py", line 139, in preloadFont
  File "pkg_resources\__init__.py", line 1133, in resource_exists
  File "pkg_resources\__init__.py", line 360, in get_provider
ModuleNotFoundError: No module named 'pyfiglet.fonts'

我似乎无法弄清楚为什么它不起作用,.py 和 .pyc 文件可以正常工作。

您应该将 pyfiglet 添加到规范文件中的数据列表中。您可以通过编辑 spec 文件自己完成此操作。否则,您可以使用

之类的东西进行构建
pyinstaller --add-data "venv\Lib\site-packages\pyfiglet;./pyfiglet" Dragon_Game.py

路径指向 PC/environment 中的 pyfiglet 文件。

参见this question