PyInstaller 打开后立即关闭程序

PyInstaller closes the program immediately after opening

对不起,我的英语不好。我使用 Google 翻译。

简单代码 — print("Hello!")

这是终端中的内容:

PS C:\OOTDDir\PythonDev\pizza> pyinstaller -F hello.py
109 INFO: PyInstaller: 4.5.1
109 INFO: Python: 3.9.6
109 INFO: Platform: Windows-10-10.0.19042-SP0
109 INFO: wrote C:\OOTDDir\PythonDev\pizza\hello.spec
109 INFO: UPX is not available.
109 INFO: Extending PYTHONPATH with paths
['C:\OOTDDir\PythonDev\pizza', 'C:\OOTDDir\PythonDev\pizza']
464 INFO: checking Analysis
465 INFO: Building Analysis because Analysis-00.toc is non existent
465 INFO: Initializing module dependency graph...
467 INFO: Caching module graph hooks...
486 INFO: Analyzing base_library.zip ...
3150 INFO: Processing pre-find module path hook distutils from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'.
3150 INFO: distutils: retargeting to non-venv dir 'c:\ootddir\python\lib'
6278 INFO: Caching module dependency graph...
6482 INFO: running Analysis Analysis-00.toc
6484 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\ootddir\python\python.exe
6546 WARNING: lib not found: api-ms-win-core-path-l1-1-0.dll dependency of c:\ootddir\python\python39.dll
6718 INFO: Analyzing C:\OOTDDir\PythonDev\pizza\hello.py
6718 INFO: Processing module hooks...
6718 INFO: Loading module hook 'hook-difflib.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
6718 INFO: Loading module hook 'hook-distutils.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
6718 INFO: Loading module hook 'hook-distutils.util.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
6734 INFO: Loading module hook 'hook-encodings.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
6812 INFO: Loading module hook 'hook-heapq.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
6812 INFO: Loading module hook 'hook-lib2to3.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
6953 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
6953 INFO: Loading module hook 'hook-pickle.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
6953 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
6953 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
6953 INFO: Loading module hook 'hook-xml.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
7015 INFO: Loading module hook 'hook-_tkinter.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
7125 INFO: checking Tree
7125 INFO: Building Tree because Tree-00.toc is non existent
7125 INFO: Building Tree Tree-00.toc
7250 INFO: checking Tree
7250 INFO: Building Tree because Tree-01.toc is non existent
7250 INFO: Building Tree Tree-01.toc
7328 INFO: checking Tree
7328 INFO: Building Tree because Tree-02.toc is non existent
7328 INFO: Building Tree Tree-02.toc
7356 INFO: Looking for ctypes DLLs
7372 INFO: Analyzing run-time hooks ...
7376 INFO: Including run-time hook 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgutil.py'
7381 INFO: Including run-time hook 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_multiprocessing.py'
7384 INFO: Including run-time hook 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_inspect.py'
7392 INFO: Looking for dynamic libraries
7536 INFO: Looking for eggs
7536 INFO: Using Python library c:\ootddir\python\python39.dll
7536 INFO: Found binding redirects:
[]
7536 INFO: Warnings written to C:\OOTDDir\PythonDev\pizza\build\hello\warn-hello.txt
7583 INFO: Graph cross-reference written to C:\OOTDDir\PythonDev\pizza\build\hello\xref-hello.html
7599 INFO: checking PYZ
7599 INFO: Building PYZ because PYZ-00.toc is non existent
7599 INFO: Building PYZ (ZlibArchive) C:\OOTDDir\PythonDev\pizza\build\hello\PYZ-00.pyz
8148 INFO: Building PYZ (ZlibArchive) C:\OOTDDir\PythonDev\pizza\build\hello\PYZ-00.pyz completed successfully.
8164 INFO: checking PKG
8164 INFO: Building PKG because PKG-00.toc is non existent
8164 INFO: Building PKG (CArchive) PKG-00.pkg
10044 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
10044 INFO: Bootloader c:\ootddir\python\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
10044 INFO: checking EXE
10060 INFO: Building EXE because EXE-00.toc is non existent
10060 INFO: Building EXE from EXE-00.toc
10062 INFO: Copying icons from ['c:\ootddir\python\lib\site-packages\PyInstaller\bootloader\images\icon-console.ico']
10062 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
10062 INFO: Writing RT_ICON 1 resource with 3752 bytes
10062 INFO: Writing RT_ICON 2 resource with 2216 bytes
10062 INFO: Writing RT_ICON 3 resource with 1384 bytes
10062 INFO: Writing RT_ICON 4 resource with 37019 bytes
10062 INFO: Writing RT_ICON 5 resource with 9640 bytes
10062 INFO: Writing RT_ICON 6 resource with 4264 bytes
10062 INFO: Writing RT_ICON 7 resource with 1128 bytes
10078 INFO: Updating manifest in C:\OOTDDir\PythonDev\pizza\build\hello\run.exe.mx2u8v5f
10078 INFO: Updating resource type 24 name 1 language 0
10078 INFO: Appending archive to EXE C:\OOTDDir\PythonDev\pizza\dist\hello.exe
11616 INFO: Building EXE from EXE-00.toc completed successfully.

删除“F”标志并不能解决问题。

PyInstaller 创建了一个文件,但它在单击时立即关闭。在我尝试 运行 我的测试游戏代码的情况下,使用 window 标志 -w,我看到一个错误: Error

在终端中是:

PS C:\OOTDDir\PythonDev\alien_invasion> pyinstaller -F alien_invasion.py
54 INFO: PyInstaller: 4.5.1
54 INFO: Python: 3.9.6
70 INFO: Platform: Windows-10-10.0.19042-SP0
70 INFO: wrote C:\OOTDDir\PythonDev\alien_invasion\alien_invasion.spec
70 INFO: UPX is not available.
70 INFO: Extending PYTHONPATH with paths
['C:\OOTDDir\PythonDev\alien_invasion',
 'C:\OOTDDir\PythonDev\alien_invasion']
305 INFO: checking Analysis
305 INFO: Building Analysis because Analysis-00.toc is non existent
305 INFO: Initializing module dependency graph...
321 INFO: Caching module graph hooks...
336 INFO: Analyzing base_library.zip ...
2691 INFO: Processing pre-find module path hook distutils from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'.
2691 INFO: distutils: retargeting to non-venv dir 'c:\ootddir\python\lib'
5835 INFO: Caching module dependency graph...
6007 INFO: running Analysis Analysis-00.toc
6022 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\ootddir\python\python.exe
6100 WARNING: lib not found: api-ms-win-core-path-l1-1-0.dll dependency of c:\ootddir\python\python39.dll
6288 INFO: Analyzing C:\OOTDDir\PythonDev\alien_invasion\alien_invasion.py
6535 INFO: Processing pre-find module path hook site from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-site.py'.
6551 INFO: site: retargeting to fake-dir 'c:\ootddir\python\lib\site-packages\PyInstaller\fake-modules'
7554 INFO: Processing module hooks...
7554 INFO: Loading module hook 'hook-pygame.py' from 'c:\ootddir\python\lib\site-packages\pygame\__pyinstaller'...
pygame 2.0.1 (SDL 2.0.14, Python 3.9.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
7601 INFO: Loading module hook 'hook-difflib.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
7617 INFO: Loading module hook 'hook-distutils.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
7617 INFO: Loading module hook 'hook-distutils.util.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
7617 INFO: Loading module hook 'hook-encodings.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
7695 INFO: Loading module hook 'hook-heapq.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
7695 INFO: Loading module hook 'hook-lib2to3.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
7868 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
7884 INFO: Loading module hook 'hook-pickle.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
7884 INFO: Loading module hook 'hook-pkg_resources.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
8167 INFO: Processing pre-safe import module hook win32com from 'c:\ootddir\python\lib\site-packages\_pyinstaller_hooks_contrib\hooks\pre_safe_import_module\hook-win32com.py'.
8443 WARNING: Hidden import "pkg_resources.py2_warn" not found!
8443 WARNING: Hidden import "pkg_resources.markers" not found!
8443 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
8443 INFO: Loading module hook 'hook-win32ctypes.core.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
8583 INFO: Loading module hook 'hook-xml.dom.domreg.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
8583 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
8583 INFO: Loading module hook 'hook-xml.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
8583 INFO: Loading module hook 'hook-_tkinter.py' from 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks'...
8694 INFO: checking Tree
8694 INFO: Building Tree because Tree-00.toc is non existent
8694 INFO: Building Tree Tree-00.toc
8757 INFO: checking Tree
8757 INFO: Building Tree because Tree-01.toc is non existent
8757 INFO: Building Tree Tree-01.toc
8820 INFO: checking Tree
8820 INFO: Building Tree because Tree-02.toc is non existent
8820 INFO: Building Tree Tree-02.toc
8851 INFO: Looking for ctypes DLLs
8882 INFO: Analyzing run-time hooks ...
8882 INFO: Including run-time hook 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgutil.py'
8898 INFO: Including run-time hook 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_multiprocessing.py'
8898 INFO: Including run-time hook 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_inspect.py'
8898 INFO: Including run-time hook 'c:\ootddir\python\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgres.py'
8914 INFO: Looking for dynamic libraries
9336 INFO: Looking for eggs
9336 INFO: Using Python library c:\ootddir\python\python39.dll
9336 INFO: Found binding redirects:
[]
9336 INFO: Warnings written to C:\OOTDDir\PythonDev\alien_invasion\build\alien_invasion\warn-alien_invasion.txt
9398 INFO: Graph cross-reference written to C:\OOTDDir\PythonDev\alien_invasion\build\alien_invasion\xref-alien_invasion.html
9414 INFO: checking PYZ
9414 INFO: Building PYZ because PYZ-00.toc is non existent
9414 INFO: Building PYZ (ZlibArchive) C:\OOTDDir\PythonDev\alien_invasion\build\alien_invasion\PYZ-00.pyz
10090 INFO: Building PYZ (ZlibArchive) C:\OOTDDir\PythonDev\alien_invasion\build\alien_invasion\PYZ-00.pyz completed successfully.
10105 INFO: checking PKG
10105 INFO: Building PKG because PKG-00.toc is non existent
10105 INFO: Building PKG (CArchive) PKG-00.pkg
12901 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
12901 INFO: Bootloader c:\ootddir\python\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
12901 INFO: checking EXE
12901 INFO: Building EXE because EXE-00.toc is non existent
12901 INFO: Building EXE from EXE-00.toc
12901 INFO: Copying icons from ['c:\ootddir\python\lib\site-packages\PyInstaller\bootloader\images\icon-console.ico']
12901 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
12901 INFO: Writing RT_ICON 1 resource with 3752 bytes
12901 INFO: Writing RT_ICON 2 resource with 2216 bytes
12901 INFO: Writing RT_ICON 3 resource with 1384 bytes
12901 INFO: Writing RT_ICON 4 resource with 37019 bytes
12901 INFO: Writing RT_ICON 5 resource with 9640 bytes
12901 INFO: Writing RT_ICON 6 resource with 4264 bytes
12901 INFO: Writing RT_ICON 7 resource with 1128 bytes
12901 INFO: Updating manifest in C:\OOTDDir\PythonDev\alien_invasion\build\alien_invasion\run.exe.m_3ze9i1
12901 INFO: Updating resource type 24 name 1 language 0
12916 INFO: Appending archive to EXE C:\OOTDDir\PythonDev\alien_invasion\dist\alien_invasion.exe
15159 INFO: Building EXE from EXE-00.toc completed successfully.
PS C:\OOTDDir\PythonDev\alien_invasion> pyinstaller -F -w alien_invasion.py
63 INFO: PyInstaller: 4.5.1
63 INFO: Python: 3.9.6
79 INFO: Platform: Windows-10-10.0.19042-SP0
79 INFO: wrote C:\OOTDDir\PythonDev\alien_invasion\alien_invasion.spec
79 INFO: UPX is not available.
79 INFO: Extending PYTHONPATH with paths
['C:\OOTDDir\PythonDev\alien_invasion',
 'C:\OOTDDir\PythonDev\alien_invasion']
318 INFO: checking Analysis
333 INFO: checking PYZ
349 INFO: checking PKG
366 INFO: Bootloader c:\ootddir\python\lib\site-packages\PyInstaller\bootloader\Windows-64bit\runw.exe
366 INFO: checking EXE
366 INFO: Building because console changed
366 INFO: Building EXE from EXE-00.toc
407 INFO: Copying icons from ['c:\ootddir\python\lib\site-packages\PyInstaller\bootloader\images\icon-windowed.ico']
407 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
407 INFO: Writing RT_ICON 1 resource with 3752 bytes
407 INFO: Writing RT_ICON 2 resource with 2216 bytes
407 INFO: Writing RT_ICON 3 resource with 1384 bytes
407 INFO: Writing RT_ICON 4 resource with 38188 bytes
407 INFO: Writing RT_ICON 5 resource with 9640 bytes
407 INFO: Writing RT_ICON 6 resource with 4264 bytes
407 INFO: Writing RT_ICON 7 resource with 1128 bytes
407 INFO: Updating manifest in C:\OOTDDir\PythonDev\alien_invasion\build\alien_invasion\runw.exe.2gd3qgqs
407 INFO: Updating resource type 24 name 1 language 0
407 INFO: Appending archive to EXE C:\OOTDDir\PythonDev\alien_invasion\dist\alien_invasion.exe
2778 INFO: Building EXE from EXE-00.toc completed successfully.

我浏览了大量网站,但没有找到解决方案。学习Python大概第二个月了,第一次遇到这么难懂的问题。说实话,我很绝望。我几乎分析完了PyGame的基本功能,想着手写自己的游戏,但我在这个问题上花了很多时间,这让我很不安。

唯一的希望就在你身上。抱歉冗长。

Eventually:

只需将包含资源的文件夹传输到包含 .exe 文件的文件夹(在我的例子中,这是包含“.bmp”文件的“图像”文件夹)。

在控制台应用程序的情况下,input 解决了同样的问题,如果将其包含在 while 循环中,也不会有问题。

如果程序在打开后立即关闭,这意味着您的代码有错误,或者一切正常,代码已完成执行。

如果您 运行 您的代码作为 .py 文件并且一切正常,这意味着问题在于代码刚刚完成执行,因此它会自动关闭。在这种情况下,您可以只添加一个简单的

while True:
    pass

无限循环,什么都不做,在你的脚本结尾。刚才说到这段代码就卡屏,打开后又不会立即关闭