How to print to stdout from Python script with .pyw extension?
Asked Answered
H

3

10

I have a python program with a wxpython GUI and some command line parameters. I generate a single windows executable with py2exe. I don't want to have a command line window in the background, so py2exe is making this a pythonw executable without this window. This is equivalent to use the *.pyw extension.

The problem is, if you want to see the available command line arguments, you naturally do "main.exe -h" on a shell. Even though argparse is providing this information, it doesn't reach stdout because of the *.pyw extension.

So how could I re-enable stdout for a GUI application using pythonw?

minimal working example:

# test.py
print "hello"

execution:

#> python test.py
hello
#> pythonw test.py
#> 

Thanks in advance for any suggestion!

Hernardo answered 24/10, 2011 at 23:44 Comment(1)
related: Windows GUI + Console Output, Linux-styleToluate
H
0

I finally solved my problem with some kind of a nasty trick. I get the help information from argparse like that:

class Parser(object):
    def __init__(self):
        [...init argparser...]
        self.help = ""
        self.__argparser.print_help(self)
    def write(self, message):
        self.help += message

Then I just show the help information in the about dialog.

I would still prefer to re-enable sys.stdout, but this works for now. Thanks to all suggestions!

Hernardo answered 9/2, 2012 at 16:32 Comment(0)
I
3

One way I've done this is use py2exe's custom-boot-script to redirect sys.stdout to a file when a certain command line switch is present.

I'll have some sample code here when I can dig it up, but check the link out to get you started.

Iridescence answered 24/10, 2011 at 23:53 Comment(2)
Thanks, but I would prefer to get sys.stdout back on the command line. Do you have an Idea how to do that?Hernardo
Unfortunately, I don't think you can do that, since the Console subsystem isn't initialized with .pyw files. You'd have to do some nasty stuff with the Win32 API AllocConsole calls to get your stdout back to a console window.Iridescence
G
1

You can tell wxPython's App instance to redirect too. Just set the "redirect" parameter to True:

app = wx.App(True)

Another solution would be to use Python's logging module instead of relying on printing strings to stdout. Using that, you can log to a file or to various web protocols, among others. See the documentation for full details: http://docs.python.org/library/logging.html

There's also a good introductory tutorial here: http://www.doughellmann.com/PyMOTW/logging/

Gilberte answered 25/10, 2011 at 13:56 Comment(0)
H
0

I finally solved my problem with some kind of a nasty trick. I get the help information from argparse like that:

class Parser(object):
    def __init__(self):
        [...init argparser...]
        self.help = ""
        self.__argparser.print_help(self)
    def write(self, message):
        self.help += message

Then I just show the help information in the about dialog.

I would still prefer to re-enable sys.stdout, but this works for now. Thanks to all suggestions!

Hernardo answered 9/2, 2012 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.