Console access from pythonw.exe

It is often useful for a non-console application to access the console when started from an interactive shell for presenting a help message, acting as a command-line client to the full graphical application, etc. This is possible by default on non-Windows platforms. A Windows application needs to choose in advance whether it will always get a console window, even when started from a graphical shell or never even get access to the console, even when started from a command line (e.g. python.exe vs pythonw.exe)

This Go code demontrates how to emulate the behavior of non-windows platforms almost perfectly:

Should this be added to pythonw.exe?

In my experience this is only needed in deep debug sessions.

I simply use python.exe not python2.exe to do that.

That code will not help on macOS, obviously, that also has a none console GUI environment.

Within my GUI apps I always catch all exceptions and log the failures with enough context to debug without a console. Also great for supporting users; as I can ask for the log and stand a good chance of fixing rare issues that I cannot repoduce.

This is messy. A CLI shell such as CMD or PowerShell doesn’t wait for a GUI application. Thus if a GUI app attaches back to the console of its parent process, its output is likely to get mixed up with output from another console process, which could be the shell or could be another app that was started after the GUI app. It’s even worse if the GUI app tries to read from the console, competing with another process for console input.