Running a process in pythonw with Popen without a console

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 13:36:02

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"])

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

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

win32api.WinExec(chrome + " " + args)
Liam

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

You might be able to just do 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.

user10426778

According to Python 2.7 documentation and Python 3.7 documentation, you can influence how Popen creates the process by setting creationflags. In particular, the CREATE_NO_WINDOW flag would be useful to you.

variable = subprocess.Popen(
   "CMD COMMAND", 
   stdout = subprocess.PIPE, creationflags = subprocess.CREATE_NO_WINDOW
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!