I often write small python apps meant to run in the background on my system, essentially CLI applications.
These apps are usually some apps using flask or bottle as my front-end interface. Python on Windows, the OS I use for my daily driver, comes with an executable that allows you to run windowless python apps, pythonw.exe.
However, pythonw.exe has a lot of limitations and is challenging to get working correctly. One limitation it has, there is no good way to launch it from a script such as a batch script without generating the same console window it was designed to avoid. It can be used with task-scheduler, or you can create modified windows shortcuts to use it with a python file.
pythonw.exe can cause compatibility issues with some apps that may rely on having access to stdout, and stderr, as any app run with it won’t have access to them correctly without overriding them.
Simply put I’ve tried to use it many times in the past and wasted hours debugging basic issues that have arisen from pythonw.exe.
All these extra steps or special conditions make it an unattractive solution.
So if you’re like me, and you are looking for a simple way to run a python app and then hide the console window, and you happen to be on windows (though I suspect there are Linux and macOS ways to do this as well), look no further.
Instead of using pythonw.exe, you can simply hide the Console Window via the Windows API using the Pywin32 module and a small amount of code. I’ve used this without issue for several years now. It is simple and gets the job done without wasting time.
# Hide console Window workaround for bottle / flask / other import win32.lib.win32con as win32con import win32gui my_app = win32gui.GetForegroundWindow() win32gui.ShowWindow(my_app, win32con.SW_HIDE)
It’s that simple, and if you ever need to show the window again, you can simply call the win32con.SW_SHOW variant.