Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing subprocess from Python without opening Windows Command Prompt [duplicate]

Possible Duplicate:
Running a process in pythonw with Popen without a console
How do I eliminate Windows consoles from spawned processes in Python (2.7)?

I have a Python program that calls a separate number-crunching program (written in C) as a subprocess several times (using subprocess.check_call). It works great on Linux, and it works great on Windows too except for one little thing: every time it calls the subprocess a Command Prompt window is created and then soon destroyed when the subprocess exits.

This doesn't affect the computation at all, but it's very annoying because this window keeps flashing on and off the screen, and it makes it difficult to do other things on the computer because the new Command Prompt window can steal keyboard focus.

How can I simply execute the subprocess (which has no GUI), and prevent the Command Prompt window from being created?

like image 431
Keenan Pepper Avatar asked Jun 17 '11 18:06

Keenan Pepper


2 Answers

How are you calling subprocess.check_call()? If you pass shell=True then the window should not be created as this will cause the SW_HIDE flag to be set for the STARTUPINFO.wShowWindow attribute.

Example:

subprocess.check_call(["ping", "google.com"], shell=True)
like image 148
Bryan Avatar answered Sep 29 '22 22:09

Bryan


When you build the C application, set its type to the Win32 Subsystem instead of the Console Subsystem. If this is a pre-built application, you could change the subsystem with this tool.

like image 45
Dark Falcon Avatar answered Sep 29 '22 23:09

Dark Falcon