Running a process in pythonw with Popen without a console
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
)
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
just do subprocess.Popen([command], shell=True)