Search code examples
pythonwindowswxpythonstdoutpythonw

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


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!


Solution

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