ActiveState Code

Recipe 409002: Launching a subprocess without a console window


On the MS Windows platform, if you launch a subprocess from within a non-console process a console window appears.

This recipe shows how to avoid this by using the python 2.4 library module subprocess.py

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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"])

Discussion

Tested on WinXP SP2

Comments

  1. 1. At 12:36 p.m. on 11 jan 2006, Antony Kummel said:

    If it doesn't work for you. And you've downloaded subprocess from effbot, then you should add this line somewhere: subprocess.STARTF_USESHOWWINDOW = 1

Sign in to comment