python 在没有控制台的情况下使用 Popen 在 pythonw 中运行进程

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

Running a process in pythonw with Popen without a console

pythonuser-interfaceconsolepopenpythonw

提问by sbirch

I have a program with a GUI that runs an external program through a Popen call:

我有一个带有 GUI 的程序,它通过 Popen 调用运行外部程序:

p = subprocess.Popen("<commands>" , stdout=subprocess.PIPE , stderr=subprocess.PIPE , cwd=os.getcwd())
p.communicate()

But a console pops up, regardless of what I do (I've also tried passing it NUL for the file handle). Is there any way to do that without getting the binary I call to free its console?

但是无论我做什么,都会弹出一个控制台(我也试过为文件句柄传递 NUL)。有没有办法在不获取我调用以释放其控制台的二进制文件的情况下做到这一点?

回答by interjay

From here:

这里

import subprocess

def launchWithoutConsole(command, args):
    """Launches 'command' windowless and waits until finished"""
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    return subprocess.Popen([command] + args, startupinfo=startupinfo).wait()

if __name__ == "__main__":
    # test with "pythonw.exe"
    launchWithoutConsole("d:\bin\gzip.exe", ["-d", "myfile.gz"])

Note that sometimes suppressing the console makes subprocess calls fail with "Error 6: invalid handle". A quick fix is to redirect stdin, as explained here: Python running as Windows Service: OSError: [WinError 6] The handle is invalid

请注意,有时抑制控制台会使子进程调用失败并显示“错误 6:句柄无效”。快速解决方法是重定向stdin,如下所述:Python running as Windows Service: OSError: [WinError 6] The handle is invalid

回答by Liam

just do subprocess.Popen([command], shell=True)

做就是了 subprocess.Popen([command], shell=True)

回答by Devon Dieffenbach

This works nicely in the win32api. The other solutions were not working for me.

这在 win32api 中运行良好。其他解决方案对我不起作用。

import win32api
chrome = "\"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe\""
args = "https://stackoverflow.com"

win32api.WinExec(chrome + " " + args)

回答by ErnestoQ

According to Python 2.7 documentationand Python 3.7 documentation, you can influence how Popencreates the process by setting creationflags. In particular, the CREATE_NO_WINDOWflag would be useful to you.

根据Python 2.7 文档Python 3.7 文档,您可以Popen通过设置creationflags. 特别是,该CREATE_NO_WINDOW标志对您很有用。

variable = subprocess.Popen(
   "CMD COMMAND", 
   stdout = subprocess.PIPE, creationflags = subprocess.CREATE_NO_WINDOW
)

回答by ThantiK

You might be able to just do subprocess.Popen([command], shell=False).

你也许能够做到subprocess.Popen([command], shell=False)

That's what I use anyways. Saves you all the nonsense of setting flags and whatnot. Once named as a .pyw or run with pythonw it shouldn't open a console.

反正我就是这么用的。为您省去设置标志之类的所有废话。一旦命名为 .pyw 或使用 pythonw 运行,它就不应该打开控制台。