2

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 am using Python 2.7 and running the python scripts from within IDLE.

The commands I am executing are simple exe's that perform quick tasks. The issue I am having is every time the external commands are called from within Python a console is created and it flashes on my screen and takes focus, thus preventing me from using my PC while executing various scripts.

Examples of how I am calling them from within Python are as follows:

result = call(["Commands\Set.exe", str(i), ARG2])
check_output(["Commands\Read.exe", ARG2])

Searching for a solution I came across adding the following

shell=True

to make the following command

check_output(["Commands\Read.exe", ARG2], shell=True)

However I still get the console appear every time an external command is called

Community
  • 1
  • 1
smashtastic
  • 323
  • 1
  • 3
  • 11

2 Answers2

4

There might be two issues here. First off, if your python scripts have the .pyw extension then they will be associated with pythonw which does not use a console*. However, you have shell=True, which generates a console*. You need to run the program and hide the console:

import subprocess
proc = subprocess.Popen('hello.py',  creationflags=subprocess.SW_HIDE, shell=True)
proc.wait()

*Pedantically, it's not a dos prompt, it is a console window. DOS - Disk Operating System - was an IBM mainframe OS. MS-DOS or PC-DOS command-line features were mirrored (with a lot of extra features) by cmd.exe (a Windows shell), which is a console program and so uses a console window. It's that console window you need to hide.

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • ok I have tried adding the creationflags=subprocess.SW_HIDE, shell=True to the check_output line and I still get a *console* window appear .... – smashtastic Apr 05 '12 at 09:36
  • another comment ... I have updated my question to reflect the console usage ... I guess for completenesss could say a "console window with dos prompt" because "C:\>" is actually a dos promopt compared to other consoles that give different prompts for instance >>>> in python ... now I guess that is being really pedantic .... – smashtastic Apr 05 '12 at 09:40
  • btw I am calling exe files not python scripts .... question updated. – smashtastic Apr 05 '12 at 09:45
  • I did say I was being pedantic ;-) Maybe the program is creating the console? – cdarke Apr 05 '12 at 10:33
  • if I run the program from the console it does nothing other than return to the command prompt after a short while (a few seconds) for the set functions or with a response with the get functions so I do not think it is the program... – smashtastic Apr 05 '12 at 12:47
  • *However, you have shell=True, which generates a console* It generates a console but it's hidden one. See my comment [here](http://stackoverflow.com/questions/1765078/1765093#comment17578243_1765093) – Piotr Dobrogost Oct 22 '12 at 21:55
2

You need to use startupinfo parameter of subprocess.Popen() class' constructor.

startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
subprocess.Popen(command, startupinfo=startupinfo)

You do not need shell=True if all you want is to hide console window; see this answer.

Community
  • 1
  • 1
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366