using subprocess.Popen does not suppress terminal window on Windows

Discussion in 'Python' started by Steven, Jun 18, 2010.

  1. Steven

    Steven Guest

    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
     
    Steven, Jun 18, 2010
    #1
    1. Advertising

  2. * 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>
     
    Alf P. Steinbach, Jun 18, 2010
    #2
    1. Advertising

  3. Steven

    Guest

    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
     
    , Sep 12, 2012
    #3
  4. Steven

    Dave Angel Guest

    Re: using subprocess.Popen does not suppress terminal window onWindows

    On 09/12/2012 11:26 AM, 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
     
    Dave Angel, Sep 13, 2012
    #4
  5. Steven

    Guest

    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.
     
    , Sep 13, 2012
    #5
  6. Re: using subprocess.Popen does not suppress terminal window on
    Windows


    On Thu, 13 Sep 2012 00:27:10 -0700 (PDT),
    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
     
    Oscar Benjamin, Sep 13, 2012
    #6
  7. Steven

    Guest

    , Sep 13, 2012
    #7
  8. Re: using subprocess.Popen does not suppress terminal window onWindows

    On Thu, 13 Sep 2012 05:33:25 -0700 (PDT),
    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/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)...

    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Sep 13, 2012
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. I. Myself
    Replies:
    2
    Views:
    502
    I. Myself
    Apr 30, 2006
  2. Tim Golden
    Replies:
    0
    Views:
    464
    Tim Golden
    Oct 7, 2007
  3. gaurav kashyap
    Replies:
    3
    Views:
    6,689
    Paul Boddie
    Oct 31, 2008
  4. Mark Shewfelt
    Replies:
    1
    Views:
    626
    yomgui
    Nov 6, 2008
  5. Steven K. Wong
    Replies:
    6
    Views:
    1,051
    Nobody
    Jan 10, 2010
Loading...

Share This Page