使用 Pyinstaller 冻结 Python 程序时摆脱控制台输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17584698/
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
Getting rid of console output when freezing Python programs using Pyinstaller
提问by dfreeze
I have recently written a fairly simple program for my grandfather using Python with GUI from Tkinter, and it works beautifully for what he will be using it for. However, there is, of course, the ugly console output window. I have successfully gotten rid of it by simply changing the extension of the file from .py to .pyw. When I freeze it using PyInstaller, it reappears again! Is there any way for me to fix this?
我最近使用 Python 和 Tkinter 的 GUI 为我的祖父编写了一个相当简单的程序,它非常适合他将使用它的目的。但是,当然还有丑陋的控制台输出窗口。我通过简单地将文件的扩展名从 .py 更改为 .pyw 就成功地摆脱了它。当我使用 PyInstaller 冻结它时,它再次出现!我有什么办法可以解决这个问题吗?
Thanks in advance.
提前致谢。
采纳答案by Stephan
If you want to hide the console window, hereis the documentation:
This is how you use the --noconsole
option
如果你想隐藏控制台窗口,这里是文档:这是你如何使用该--noconsole
选项
python pyinstaller.py --noconsole yourscript.py
If you need help using pyinstaller to get to the point where you need to use the --noconsole
option hereis a simple tutorial for getting there.
如果您需要使用 pyinstaller 的帮助来达到您需要使用该--noconsole
选项的程度,这里是一个简单的教程。
回答by Blender
Just add the --noconsole
flag:
只需添加--noconsole
标志:
$ python pyinstaller.py --noconsole yourprogram.py
You might want to use --onefile
as well, which creates a single .exe
file instead of a folder.
您可能还想使用--onefile
它,它会创建单个.exe
文件而不是文件夹。
回答by PileUpOKpull
This is one of the first things that comes up in a search for this info, so I'd like to add what I found for release 3.2 of pyinstaller. If you've already packaged your script by running
这是搜索此信息时首先出现的内容之一,因此我想添加我在 pyinstaller 3.2 版中找到的内容。如果您已经通过运行打包脚本
pyinstaller --onefile your_script.py
or similar, you can edit the your_script.spec file to rid yourself of the console.
或类似的,您可以编辑 your_script.spec 文件以摆脱控制台。
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='main',
debug=False,
strip=False,
upx=True,
console=True )
Simply change the console value to False. Then run:
只需将控制台值更改为 False。然后运行:
pyinstaller your_script.spec
Additionally, if you make changes to your code, run the above command to have them reflected in the your_script.exe. I have found this useful for debugging various other issues.
此外,如果您对代码进行了更改,请运行上述命令以将它们反映在 your_script.exe 中。我发现这对于调试各种其他问题很有用。
回答by AsHoka
Pyinstaller -F --noconsole yourfilename.pyw
This will create a single .exe file
这将创建一个单一的 .exe 文件
Pyinstaller --noconsole yourfilename.pyw
Using this you will get the .exe file along with all .dll and other necessary files in a folder.
使用它,您将获得 .exe 文件以及文件夹中的所有 .dll 和其他必要文件。