在 PyInstaller 中使用 --onefile 和 .spec

Using --onefile with a .spec in PyInstaller

我是 "compiling" 一个使用 PyInstaller 使用 .spec 文件的程序。我正在使用 .spec 文件,因为我需要在程序中包含一个额外的文件。当我尝试执行 PyInstaller --onefile Prog.spec 时,它仍然在 dist 中创建一个文件夹,所有文件都分开,而不是像我期望的那样创建一个文件。如果我执行 PyInstaller --onefile Prog.py,那么它确实会在 dist 中创建一个 .exe 文件,这正是我想要的。使用 .spec 文件时,我需要做什么特别的事情吗?

您可以在命令行上添加额外的文件,而不是编辑规范文件:

pyinstaller --onefile --add-data <SRC;DEST or SRC:DEST> yourfile.py

否则,请确保规范文件中没有收集步骤:

"In one-file mode, there is no call to COLLECT, and the EXE instance receives all of the scripts, modules and binaries."

https://pyinstaller.readthedocs.io/en/stable/usage.html 有关命令行标志的更多信息。

如果问题仍然存在,这也可能提供一些见解:Bundling data files with PyInstaller (--onefile)

使用 pyi-makespec --onefile yourprogram.py 为 onefile 模式生成示例规范文件。

https://pyinstaller.readthedocs.io/en/stable/man/pyi-makespec.html


没有COLLECT调用,EXE调用不同。示例:

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='main',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )