首页 > 解决方案 > PyInstaller 陷入无限循环(钩子)

问题描述

我正在尝试为我的游戏制作一个启动器。我已经成功制作了可以自动更新我的游戏的部件。我现在要做的是使用 PyInstaller 将生成的(或更新的)python 代码转换为可执行文件。我正在使用此代码来执行此操作:

from PyInstaller.__main__ import run
[code that generates main.py and it's dependencies]
run(["main.py", "--onefile", "-w"])

当我将启动器作为 python 文件运行时,它可以像我预期的那样完美运行。但是在我使用 PyInstaller 将launcher.py 转换为launcher.exe 之后,它就不再起作用了。错误是:

Traceback (most recent call last):
  File "launcher.py", line 5, in <module>
    from PyInstaller.__main__ import run
  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 540, in exec_module
  File "PyInstaller\__init__.py", line 55, in <module>
  File "pkg_resources\__init__.py", line 480, in get_distribution
  File "pkg_resources\__init__.py", line 356, in get_provider
  File "pkg_resources\__init__.py", line 899, in require
  File "pkg_resources\__init__.py", line 785, in resolve
pkg_resources.DistributionNotFound: The 'PyInstaller' distribution was not found and is required by the application
[16788] Failed to execute script launcher

我做了一些研究,发现我必须为 PyInstaller 本身添加一个钩子。我这样做了,然后出现了另一条错误消息:

 File "launcher.py", line 55, in <module>
    run(["main.py", "--onefile", "-w"])
  File "PyInstaller\__main__.py", line 81, in run
  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 540, in exec_module
  File "PyInstaller\building\build_main.py", line 35, in <module>
  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 540, in exec_module
  File "PyInstaller\depend\bindepend.py", line 30, in <module>
  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 540, in exec_module
  File "PyInstaller\depend\utils.py", line 28, in <module>
  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 540, in exec_module
  File "PyInstaller\lib\modulegraph\modulegraph.py", line 29, in <module>
  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 540, in exec_module
  File "altgraph\__init__.py", line 144, in <module>
  File "pkg_resources\__init__.py", line 899, in require
  File "pkg_resources\__init__.py", line 785, in resolve
pkg_resources.DistributionNotFound: The 'altgraph' distribution was not found and is required by the application
[6716] Failed to execute script launcher

再次,我为“altgraph”添加了一个钩子,然后发生了这种情况:

5204 INFO: PyInstaller: 4.3
5204 INFO: Python: 3.9.4
5212 INFO: Platform: Windows-10-10.0.18362-SP0
5212 INFO: wrote E:\编程\Python\Still working\2D Minecraft\Launcher test\main.spec
5220 INFO: UPX is not available.
5220 INFO: Extending PYTHONPATH with paths
['E:\\编程\\Python\\Still working\\2D Minecraft\\Launcher test',
 'E:\\编程\\Python\\Still working\\2D Minecraft\\Launcher test']
5220 INFO: checking Analysis
5228 INFO: Building Analysis because Analysis-00.toc is non existent
5236 INFO: Initializing module dependency graph...
5236 INFO: Caching module graph hooks...
5236 INFO: Analyzing base_library.zip ...
5342 INFO: PyInstaller: 4.3
5342 INFO: Python: 3.9.4
5342 INFO: Platform: Windows-10-10.0.18362-SP0
5350 INFO: wrote E:\编程\Python\Still working\2D Minecraft\Launcher test\main.spec
5350 INFO: UPX is not available.
5358 INFO: Extending PYTHONPATH with paths
['E:\\编程\\Python\\Still working\\2D Minecraft\\Launcher test',
 'E:\\编程\\Python\\Still working\\2D Minecraft\\Launcher test']
5366 INFO: checking Analysis
5366 INFO: Building Analysis because Analysis-00.toc is non existent
5366 INFO: Initializing module dependency graph...
5374 INFO: Caching module graph hooks...
5382 INFO: Analyzing base_library.zip ...
7309 INFO: PyInstaller: 4.3
7309 INFO: Python: 3.9.4
7309 INFO: Platform: Windows-10-10.0.18362-SP0
7317 INFO: wrote E:\编程\Python\Still working\2D Minecraft\Launcher test\main.spec
7325 INFO: UPX is not available.
7325 INFO: Extending PYTHONPATH with paths
['E:\\编程\\Python\\Still working\\2D Minecraft\\Launcher test',
 'E:\\编程\\Python\\Still working\\2D Minecraft\\Launcher test']
7333 INFO: checking Analysis
7333 INFO: Building Analysis because Analysis-00.toc is non existent
7333 INFO: Initializing module dependency graph...
7341 INFO: Caching module graph hooks...
7349 INFO: Analyzing base_library.zip ...
6976 INFO: PyInstaller: 4.3
6976 INFO: Python: 3.9.4
6984 INFO: Platform: Windows-10-10.0.18362-SP0
6984 INFO: wrote E:\编程\Python\Still working\2D Minecraft\Launcher test\main.spec
6992 INFO: UPX is not available.
6992 INFO: Extending PYTHONPATH with paths
['E:\\编程\\Python\\Still working\\2D Minecraft\\Launcher test',
 'E:\\编程\\Python\\Still working\\2D Minecraft\\Launcher test']
7000 INFO: checking Analysis
7000 INFO: Building Analysis because Analysis-00.toc is non existent
7000 INFO: Initializing module dependency graph...
7000 INFO: Caching module graph hooks...
7008 INFO: Analyzing base_library.zip ...

它进入了一个无限循环,重复这些消息:

6976 INFO: PyInstaller: 4.3
6976 INFO: Python: 3.9.4
6984 INFO: Platform: Windows-10-10.0.18362-SP0
6984 INFO: wrote E:\编程\Python\Still working\2D Minecraft\Launcher test\main.spec
6992 INFO: UPX is not available.
6992 INFO: Extending PYTHONPATH with paths
['E:\\编程\\Python\\Still working\\2D Minecraft\\Launcher test',
 'E:\\编程\\Python\\Still working\\2D Minecraft\\Launcher test']
7000 INFO: checking Analysis
7000 INFO: Building Analysis because Analysis-00.toc is non existent
7000 INFO: Initializing module dependency graph...
7000 INFO: Caching module graph hooks...
7008 INFO: Analyzing base_library.zip ...

我让它运行了一段时间,它一直在运行(请注意,我的游戏中没有导入 altgraph,所以我在那部分也有点困惑)

标签: pythonpyinstaller

解决方案


推荐阅读