windows 如何避免包含 os.system 调用的 .pyw 文件的控制台窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1765078/
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-09-15 13:28:05  来源:igfitidea点击:

How to avoid console window with .pyw file containing os.system call?

pythonwindowsconsolewindows-consolepythonw

提问by twneale

If I save my code files as .pyw, no console window appears - which is what I want - but if the code includes a call to os.system, I still get a pesky console window. I assume it's caused by the call to os.system. Is there a way to execute other files from within my .pywscript without raising the console window at all?

如果我将代码文件另存为.pyw,则不会出现控制台窗口——这正是我想要的——但如果代码包含对 的调用os.system,我仍然会看到一个讨厌的控制台窗口。我认为这是由调用os.system. 有没有办法从我的.pyw脚本中执行其他文件而根本不提升控制台窗口?

采纳答案by robince

You could try using the subprocessmodule (subprocess.Popen, subprocess.callor whatever) with the argument shell=Trueif you want to avoid starting a console window.

如果您想避免启动控制台窗口,您可以尝试使用带有参数的子进程模块(subprocess.Popensubprocess.call或其他)shell=True

回答by Piotr Dobrogost

You should use subprocess.Popenclass passing as startupinfoparameter's value instance of subprocess.STARTUPINFOclass with dwFlagsattribute holding subprocess.STARTF_USESHOWWINDOWflag and wShowWindowattribute holding subprocess.SW_HIDEflag. This can be inferred from reading lines 866-868of subprocess.pysource code. It might be necessary to also pass subprocess.CREATE_NEW_CONSOLEflag as a value of creationflagsparameter as you run under pythonw.exewhich does not open a console.

您应该使用subprocess.Popen类传递作为具有属性保持标志和属性保持标志startupinfosubprocess.STARTUPINFO类的参数值实例。这可以从读取线来推断866-868的源代码。当您在不打开控制台的情况下运行时,可能还需要将标志作为参数值传递。dwFlagssubprocess.STARTF_USESHOWWINDOWwShowWindowsubprocess.SW_HIDEsubprocess.pysubprocess.CREATE_NEW_CONSOLEcreationflagspythonw.exe

When you use shell=Trueit just happens that all of the above is set correctly but that doesn't mean it's a proper solution. I would argue it's not because it adds overhead of running command interpreter and parsing arguments. In addition you should keep in mind that (...) the use of shell=True is strongly discouragedin cases where the command string is constructed from external inputaccording to documentation of subprocess module.

当您使用shell=True它时,上述所有内容都设置正确,但这并不意味着它是一个正确的解决方案。我认为这不是因为它增加了运行命令解释器和解析参数的开销。此外,您应该记住,根据子流程模块的文档从外部输入构造命令字符串的情况下,强烈建议不要使用 (...) shell=True

回答by Frank S. Thomas

The solution that Piotr describes is actually not as complicated as it may sound. Here is an example where a startupinfois passed to a check_callinvocation to suppress the console window:

Piotr 描述的解决方案实际上并不像听起来那么复杂。这是一个将 astartupinfo传递给check_call调用以抑制控制台窗口的示例:

startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

subprocess.check_call(cmd, startupinfo=startupinfo)

Since the convenience functions call, check_call, and check_outputforward their **kwargsto the Popenconstructor, it is not required to use Popendirectly.

由于方便函数callcheck_callcheck_output将它们转发**kwargsPopen构造函数,因此不需要Popen直接使用。

回答by showi

People are a bit lazy... I would thx @Piotr Dobrogostand @Frank S. Thomasfor their answers.

人们有点懒惰......我会感谢@Piotr Dobrogost@Frank S. Thomas的回答。

I came with this code who is runinng on Linux and Windows:

我带着这个在 Linux 和 Windows 上运行的代码来了:

import platform
import subprocess
startupinfo = None
if platform.system() == 'Windows':
    import _subprocess  # @bug with python 2.7 ?
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
    startupinfo.wShowWindow = _subprocess.SW_HIDE

Later...

之后...

args = [exe, ...]
out = subprocess.check_output(args, startupinfo=startupinfo)

Thx guys ;)

谢谢伙计们;)

Additionally: just to note that the following code using 'call' also works on Python 2.7 (on Windows) with the 'startupinfo' code above:

另外:请注意,以下使用“call”的代码也适用于带有“startupinfo”代码的 Python 2.7(在 Windows 上):

def run_command(cmd, sin, sout):
    print "Running cmd : %s"%(" ".join(cmd) )
    return subprocess.call( cmd, stdin=sin, stdout=sout, startupinfo=startupinfo)

回答by yuliskov

It seems that 'os.popen' doesn't produce console window. Script is running under 'pythonw'. Not sure about all cases but in my case it works well.

似乎 'os.popen' 不会产生控制台窗口。脚本在“pythonw”下运行。不确定所有情况,但在我的情况下它运行良好。

os.popen(command)

回答by DevPlayer

Similar to what @firsthandsaid, I've read on the wxPython-user forums that you "replace" the current running application, that would be "command.com" or "CMD.exe", with pyw.exe or pythonw.exe when you use something like the following:

@firsthand所说的类似,我在 wxPython-user 论坛上读到,你用 pyw.exe 或 pythonw.exe“替换”了当前正在运行的应用程序,即“command.com”或“CMD.exe”当您使用以下内容时:

os.execl(sys.executable, *([sys.executable]+sys.argv))

see another post

另一个帖子

Although I do not know how you would pipe io in this case.

虽然我不知道在这种情况下您将如何通过管道传输 io。

I believe one benefit of this approach is if you run your script multiple times your OS taskbar with not fill up with CMD icons. The other way if you have several CMD minimized in the taskbar and start closing them, it is impossible to tell which CMD goes with which pythonw script.

我相信这种方法的一个好处是,如果您在操作系统任务栏上多次运行脚本而不用 CMD 图标填充。另一方面,如果您在任务栏中最小化了多个 CMD 并开始关闭它们,则无法分辨哪个 CMD 与哪个 pythonw 脚本搭配使用。