9

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?

Community
  • 1
  • 1
Keenan Pepper
  • 321
  • 1
  • 3
  • 10
  • 1
    Are you using shell=True when calling `subprocess.check_call` ? if yes try to not use it and see if the behavior didn't change. – mouad Jun 17 '11 at 18:54
  • See http://stackoverflow.com/questions/1016384/cross-platform-subprocess-with-hidden-window – JacquesB Feb 07 '12 at 18:37

2 Answers2

2

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.

Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • How do I do this in Visual C++ 2010 Express? – Keenan Pepper Jun 17 '11 at 19:08
  • OK, I found that among the linker options, but now it says I need to define some function called _WinMain – Keenan Pepper Jun 17 '11 at 19:11
  • You need to rename your `main()` to `WinMain()`. Function signature: http://msdn.microsoft.com/en-us/library/ff381406%28v=vs.85%29.aspx – Dark Falcon Jun 17 '11 at 19:26
  • Well, I can't just rename it, because uses command line arguments. I guess the function CommandLineToArgvW will come in handy. This is way more of a pain than I would have thought... Anyway, thanks for the hint. – Keenan Pepper Jun 17 '11 at 19:29
2

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)
Bryan
  • 6,529
  • 2
  • 29
  • 16
  • This is also somewhat of a pain because it doesn't just work; it has some error now because it doesn't like when I give it a relative path, and if I give it an absolute path it doesn't like that either because it has spaces in it... – Keenan Pepper Jun 17 '11 at 19:46
  • I ended up just passing in a STARTUPINFO manually. Worked like a charm. – Keenan Pepper Jun 17 '11 at 20:35
  • 7
    Please do not promote `shell=True` as the solution just because it happens to hide console. See my answer [here](http://stackoverflow.com/a/12964900/95735). – Piotr Dobrogost Oct 22 '12 at 20:51