Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I eliminate Windows consoles from spawned processes in Python (2.7)? [duplicate]

Possible Duplicate:
Running a process in pythonw with Popen without a console

I'm using python 2.7 on Windows to automate batch RAW conversions using dcraw and PIL.

The problem is that I open a windows console whenever I run dcraw (which happens every couple of seconds). If I run the script using as a .py it's less annoying as it only opens the main window, but I would prefer to present only the GUI.

I'm involving it like so:

args = [this.dcraw] + shlex.split(DCRAW_OPTS) + [rawfile]
proc = subprocess.Popen(args, -1, stdout=subprocess.PIPE)
ppm_data, err = proc.communicate()
image = Image.open(StringIO.StringIO(ppm_data))

Thanks to Ricardo Reyes

Minor revision to that recipe, in 2.7 it appears that you need to get STARTF_USESHOWWINDOW from _subprocess (you could also use pywin32 if you want something that might be a little less prone to change), so for posterity:

suinfo = subprocess.STARTUPINFO()
suinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(args, -1, stdout=subprocess.PIPE, startupinfo=suinfo)
like image 333
Olson Avatar asked Aug 02 '10 18:08

Olson


1 Answers

You need to set the startupinfo parameter when calling Popen.

Here's an example from an Activestate.com Recipe:

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"])
like image 111
Ricardo Reyes Avatar answered Oct 20 '22 06:10

Ricardo Reyes