Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using subprocess.Popen does not suppress terminal window on Windows

317 views
Skip to first unread message

Steven

unread,
Jun 18, 2010, 12:23:19 PM6/18/10
to
I am calling a ruby program from a python gui and using
subprocess.Popen in Windows XP using python 2.6. Unfortunately,
whenever the ruby program is called a blank command window appears on
screen, annoying my users. Is there a way to suppress this behaviour?

Below is a minimal program that demonstrates the problem. The problem
does not manifest if the python program is launched via the command
line. To duplicate launch from Windows Explorer by double-clicking on
the python file.

--- call_double.pyw ---
from subprocess import *
import time

time.sleep(3) # to show that command window is result of call to Popen
p = Popen(['ruby.exe', 'double.rb'], stdin=PIPE, stdout=PIPE,
stderr=PIPE)
results = open('results.txt', 'w')
for n in range(10):
p.stdin.write("%d\n" % n)
result = p.stdout.readline().strip()
results.write('double(%s) => %2s\n' % (n, result))
results.close()

--- end of call_double.pyw ---

--- double.rb ---
while true
puts $stdin.gets().strip!.to_i * 2
STDOUT.flush
end
--- end of double.rb ---

thanks for any help,
Steven Rumbalski

Alf P. Steinbach

unread,
Jun 18, 2010, 12:40:13 PM6/18/10
to
* Steven, on 18.06.2010 18:23:

> I am calling a ruby program from a python gui and using
> subprocess.Popen in Windows XP using python 2.6. Unfortunately,
> whenever the ruby program is called a blank command window appears on
> screen, annoying my users. Is there a way to suppress this behaviour?

Yes, launch the GUI subsystem Ruby interpreter.


<example of="finding that beast">
C:\projects\blog\cppx\exception_translation\examples> set pathe
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.CJS;.JS;.JSE;.WSF;.WSH;.RB;.RBW

C:\projects\blog\cppx\exception_translation\examples> assoc .rb
.rb=rbFile

C:\projects\blog\cppx\exception_translation\examples> ftype rbfile
rbfile="C:\PROGRA~1\@utilities\ruby\bin\ruby.exe" "%1" %*

C:\projects\blog\cppx\exception_translation\examples> assoc .rbw
.rbw=rbwFile

C:\projects\blog\cppx\exception_translation\examples> ftype rbwfile
rbwfile="C:\PROGRA~1\@utilities\ruby\bin\rubyw.exe" "%1" %*

C:\projects\blog\cppx\exception_translation\examples> _
</example>


OK, it's called 'rubyw.exe'.

You can read about console and GUI subsystem for the complete beginner
programmer at <url: http://tinyurl.com/programmingbookP3>, ch. 1 (hope I got the
URL right).


> Below is a minimal program that demonstrates the problem. The problem
> does not manifest if the python program is launched via the command
> line. To duplicate launch from Windows Explorer by double-clicking on
> the python file.
>
> --- call_double.pyw ---
> from subprocess import *
> import time
>
> time.sleep(3) # to show that command window is result of call to Popen
> p = Popen(['ruby.exe', 'double.rb'], stdin=PIPE, stdout=PIPE,
> stderr=PIPE)

Change this to 'rubyw.exe' when running in Windows.

Note that that it's perfectly OK to pipe to or from a GUI subsystem program.


> results = open('results.txt', 'w')
> for n in range(10):
> p.stdin.write("%d\n" % n)
> result = p.stdout.readline().strip()
> results.write('double(%s) => %2s\n' % (n, result))
> results.close()
>
> --- end of call_double.pyw ---
>
> --- double.rb ---
> while true
> puts $stdin.gets().strip!.to_i * 2
> STDOUT.flush
> end


Cheers & hth.,

- Alf

--
blog at <url: http://alfps.wordpress.com>

janis.j...@gmail.com

unread,
Sep 12, 2012, 11:26:34 AM9/12/12
to
Hi, and I'm sorry for using this old thread, but I'm experiencing the same problem, except, that I wan't to execute any shell script or exe without blank terminal window.

Is there any way in python to supress blank console screen while script is executing? Multiplatform solution would be nice.

With best regards, Jānis.
I

Dave Angel

unread,
Sep 12, 2012, 7:49:31 PM9/12/12
to janis.j...@gmail.com, pytho...@python.org
On 09/12/2012 11:26 AM, janis.j...@gmail.com wrote:
> Hi, and I'm sorry for using this old thread, but I'm experiencing the same problem, except, that I wan't to execute any shell script or exe without blank terminal window.
> Is there any way in python to supress blank console screen while script is executing? Multiplatform solution would be nice.
> With best regards, Jānis.
>

it's not clear from your question: Are you launching non-Python
programs from a python one, using Popen? If so, I can't help. It's the
launchee that determines if a console is created, as best as I know.
However, if you're trying to launch a python program without its getting
a console, then read on.

No need for a multiplatform solution, since the problem is a Windows
one. Windows will create a console for a new process unless the parent
console is still available (eg. you run it from command line) or unless
the executable is marked with the "no console" flag. (I don't recall
what that's actually called, I haven't used Windows in a long time).

Anyway, in the Windows version, there are two executables python.exe
and pythonw.exe. You want the latter one, either by explicitly naming
it in your launcher (batch file, script, whatever), or by using the .pyw
extension, which is normally associated with the pythonw.exe program.

--

DaveA

janis.j...@gmail.com

unread,
Sep 13, 2012, 3:27:10 AM9/13/12
to
Thanks for answer, but that's not helping.

I'm making a little embedded system programming IDE so I need to run .exe(windows only), make commands, perl & python scripts etc(multiplatform). I'm using subprocess.Popen for all of them and it works fine except that blank console window and btw it pop's out under linux too.

Maybe the problem is that original python script has .pyw extension, so it hides his own console, but I don't need thatone too.

P.S. If it makes a diffrence I'm using wxPython 2.9. & Python 2.7.2.

Oscar Benjamin

unread,
Sep 13, 2012, 5:22:45 AM9/13/12
to pytho...@python.org
On Thu, 13 Sep 2012 00:27:10 -0700 (PDT), janis.j...@gmail.com
wrote:
Perhaps wxPython is causing the problem. Does the 'terminal' look
like a normal terminal? Does it only appear if you actually print
something?

Oscar

janis.j...@gmail.com

unread,
Sep 13, 2012, 8:33:25 AM9/13/12
to
It looks like normal terminal to me, could You define normal?

Looks like it appears only when target script prints something, but it shouldn't cus I'm using pipes on stdout and stderr.

If anyone is interested I'm using function doPopen from here: http://code.google.com/p/mansos/source/browse/trunk/tools/IDE/src/helperFunctions.py
Message has been deleted
0 new messages