윈도우

wxPython Phoenix: The script runs fine with python fine, doesn't run with pythonw

ForceCore 2014. 10. 11. 16:40

I've had a serious problem that my simple GUI app would run as myscript.py fine, but will not run as myscript.pyw. Why pyw? Because having a GUI app with debug console hanging around would not look very pleasant to the users.


By manually running from command console:

python myscript.pyw -> runs OK.

pythonw myscript.pyw -> Doesn't run, no error message what-so-ever.

Even after packing with py2exe, the same thing would happen. The program runs find with the console, but will not do ANYTHING without the console, no error message, no file writing and so on.


http://stackoverflow.com/questions/24835155/pyw-and-pythonw-does-not-run-under-windows-7


Even after redirecting the pipes, it didn't work.




After hours of trying, I've found that the statements below import wx will not work, I believe that is where some kind of error was occuring. I've imported some print to file statements before import wx line and that ran fine.


The solution was to redirect stdout and stderror to non-console streams BEFORE importing wx!

Before fixing, the import statements were like this:

#!/usr/bin/python3

# -*- coding: utf8 -*-

import sys

import os

import wx

import wx.adv



To fix the problem, redirection has been added.

#!/usr/bin/python3

# -*- coding: utf8 -*-

import sys

import os


# we need to redirect some pipes BEFORE importing wx.

sys.stdout = open(os.devnull, 'w')

sys.stderr = open(os.devnull, 'w')

import wx

import wx.adv


Voila!


I think this problem is unique to wxPython Phoenix (for Python 3), because I still have a running Python 2 script that works without redirection (Python 2 + wxPython).