using subprocess.Popen does not suppress terminal window on Windows

S

Steven

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
 
A

Alf P. Steinbach

* 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
 
J

janis.judvaitis

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 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

I
 
D

Dave Angel

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.
 
J

janis.judvaitis

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'musing subprocess.Popen for all of them and it works fine except that blankconsole 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.
 
O

Oscar Benjamin

Re: using subprocess.Popen does not suppress terminal window on
Windows


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.

Perhaps wxPython is causing the problem. Does the 'terminal' look
like a normal terminal? Does it only appear if you actually print
something?

Oscar
 
D

Dennis Lee Bieber

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

What does

E:\UserData\Wulfraed\My Documents>echo %comspec%
C:\WINDOWS\system32\cmd.exe

show for your system (not that I'm expecting much difference -- as long
as it isn't the 16-bit command.com <G>)

Is there any chance your spawned programs are opening/reading STDIN?
That would be sufficient to cause Windows to open a console window. You
may need to specify a pipe for STDIN.

You may also want to examine (in Python 2.7) chapter 17.1.3 "Windows
Popen Helpers"

OR, maybe
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682528(v=vs.85).aspx
"""
The system creates a new console when it starts a console process, a
character-mode process whose entry point is the main function. For
example, the system creates a new console when it starts the command
processor. When the command processor starts a new console process, the
user can specify whether the system creates a new console for the new
process or whether it inherits the command processor's console.
"""

Note that the console creation is a function of the Windows
operating system, and not of the Python subprocess library. Also, if you
main process is running under pythonw (explicitly, or via a .pyw
extension) your main process itself does not have a console to inherit
(pythonw uses, I believe, the WinMain entry point which identifies a
graphical interface).

Some Googling indicates that ActiveState's Perl includes a wperl.exe
which behaves similar to pythonw.exe -- run as a graphical application,
not a console application. However -- I don't know if you can thereby
use pipes for communication (graphical application doesn't have
stdin/stdout)...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top