Python 在 PyInstaller 中使用带有 .spec 的 --onefile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47143315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:01:40  来源:igfitidea点击:

Using --onefile with a .spec in PyInstaller

pythonpyinstaller

提问by TheStrangeQuark

I'm "compiling" a program using PyInstaller using a .spec file. I'm using the .spec file because I need to include an extra file in the program. When I try to do PyInstaller --onefile Prog.spec, it still makes a folder in distwith all the files separate instead of making a single file as I'd expect. If I do PyInstaller --onefile Prog.pythen it does make a single .exe file in dist, which is what I want. Is there something special I need to do when using a .spec file?

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

采纳答案by The4thIceman

You can add the extra file on the command line instead of editing the spec file:

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

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

Otherwise, make sure in the spec file there is no collect step:

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

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

“在单文件模式下,不调用 COLLECT,EXE 实例接收所有脚本、模块和二进制文件。”

https://pyinstaller.readthedocs.io/en/stable/usage.htmlfor more info on command line flags.

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

This also may offer some insight if problems persist: Bundling data files with PyInstaller (--onefile)

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

回答by Alan L

Use pyi-makespec --onefile yourprogram.pyto generate a sample spec file for onefile mode.

使用pyi-makespec --onefile yourprogram.py来产生onefile模式样本规范文件。

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

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



There is no COLLECT call, and the EXE call is different. Example:

没有 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 )