How can I freeze an application with two modes (GUI and console) using cx_Freeze?

I developed a Python application that works both in GUI mode and in console mode. If any arguments are specified, it starts in console mode, otherwise it starts in GUI mode.

I managed to freeze this using cx_Freeze. I had some problems hiding the black console window that appeared with wxPython, so I changed my setup.py script as follows:

 import sys from cx_Freeze import setup, Executable base = None if sys.platform == "win32": base = "Win32GUI" setup( name = "simple_PyQt4", version = "0.1", description = "Sample cx_Freeze PyQt4 script", executables = [Executable("PyQt4app.py", base = base)]) 

This works fine, but now when I try to open the console and run the executable, it does not output anything. I am not getting any errors or messages, so it seems that cx_Feeze redirects stdout to another place.

Is it possible to make it work with both modes? Nothing like this seems to be documented anywhere. :(

Thanks in advance.

Mridang

+6
source share
2 answers

I found this bit on this page :

Tip for the consoleless version: If you try to print anything, you will get an unpleasant error window because stdout and stderr do not exist (and cx_freeze Win32gui.exe stub will display an error window). This is a pain when you want your program to be able to run in GUI mode and command line mode. To safely disable console output, do the following at the beginning of your program:

 try: sys.stdout.write("\n") sys.stdout.flush() except IOError: class dummyStream: ''' dummyStream behaves like a stream but does nothing. ''' def __init__(self): pass def write(self,data): pass def read(self,data): pass def flush(self): pass def close(self): pass # and now redirect all default streams to this dummyStream: sys.stdout = dummyStream() sys.stderr = dummyStream() sys.stdin = dummyStream() sys.__stdout__ = dummyStream() sys.__stderr__ = dummyStream() sys.__stdin__ = dummyStream() 

Thus, if the program runs in console mode, it will work even if the code contains print instructions. And if it starts in command line mode, it will print as usual. (This is basically what I did in webGobbler, too.)

+13
source

Raymond Chen wrote about this: http://blogs.msdn.com/b/oldnewthing/archive/2009/01/01/9259142.aspx . In short, this is not possible directly under Windows, but there are some workarounds.

I would suggest sending two executable files - cli and gui.

+2
source

All Articles