Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > using subprocess.Popen does not suppress terminal window on Windows

Reply
Thread Tools

using subprocess.Popen does not suppress terminal window on Windows

 
 
Steven
Guest
Posts: n/a
 
      06-18-2010
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-18-2010
* 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\exampl es> set pathe
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.CJS;.JS;.JS E;.WSF;.WSH;.RB;.RBW

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

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

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

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

C:\projects\blog\cppx\exception_translation\exampl es> _
</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>
 
Reply With Quote
 
 
 
 
janis.judvaitis@gmail.com
Guest
Posts: n/a
 
      09-12-2012
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.

On Friday, June 18, 2010 7:23:19 PM UTC+3, Steven wrote:
> 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
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      09-12-2012
On 09/12/2012 11:26 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
janis.judvaitis@gmail.com
Guest
Posts: n/a
 
      09-13-2012
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.

 
Reply With Quote
 
Oscar Benjamin
Guest
Posts: n/a
 
      09-13-2012
On Thu, 13 Sep 2012 00:27:10 -0700 (PDT), (E-Mail Removed)
wrote:
> 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

 
Reply With Quote
 
janis.judvaitis@gmail.com
Guest
Posts: n/a
 
      09-13-2012
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/sour...erFunctions.py
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      09-13-2012
On Thu, 13 Sep 2012 05:33:25 -0700 (PDT), (E-Mail Removed)
declaimed the following in gmane.comp.python.general:

> 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/sour...erFunctions.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/libr...=vs.85%29.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)...

--
Wulfraed Dennis Lee Bieber AF6VN
(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
v = vte.Terminal() AttributeError: 'module' object has no attribute 'Terminal' Steve Python 2 12-07-2010 05:48 PM
open a new terminal window from another terminal window in linux/unixsystem gaurav kashyap Python 3 10-31-2008 12:10 PM
suppress opening command window after using os.system command boriq Python 4 06-12-2008 10:42 AM



Advertisments