py and pyw behave differently for PyQt4 script if print function is presented
Python 2.7.8 32 bit
Windows 8.1 Enterprise 32 bit
If PyQt4 script contains print function then pyw fail to launch the script, while py work just fine
-
command
py test.py привет
result -
command
pyw test.py привет
result: nothing at all, no window, no errors
However if comment out line 34 (print function) then pyw works just fine.
test.py:
#!python2
from __future__ import print_function
import ctypes, sys
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('MarkupViewer')
# enable unicode filenames
from ctypes import POINTER, byref, cdll, c_int, windll
from ctypes.wintypes import LPCWSTR, LPWSTR
GetCommandLineW = cdll.kernel32.GetCommandLineW
GetCommandLineW.argtypes = []
GetCommandLineW.restype = LPCWSTR
CommandLineToArgvW = windll.shell32.CommandLineToArgvW
CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
CommandLineToArgvW.restype = POINTER(LPWSTR)
cmd = GetCommandLineW()
argc = c_int(0)
argv = CommandLineToArgvW(cmd, byref(argc))
if argc.value > 0:
# Remove Python executable and commands if present
start = argc.value - len(sys.argv)
sys.argv = [argv[i] for i in xrange(start, argc.value)]
from PyQt4 import QtGui
class test(QtGui.QLabel):
def __init__(self, *args):
QtGui.QLabel.__init__(self, None)
self.text = args[0]
print(self.text)
self.setText(self.text)
self.show()
text = sys.argv[1]
app = QtGui.QApplication(sys.argv)
test = test(text)
app.exec_()
Comments (5)
-
repo owner -
Account Deactivated reporter what happens then?
It fixes the issue.
But can it be resolve in pyw (or maybe some kind of envvar)? I mean wrapping print each time is kinda tedious.
-
Account Deactivated reporter I’ve replace print with
try: print(self.text) except Exception as e: self.text += str(e)
result:
Calling
set PYTHONIOENCODING=utf8 & pyw test.py привет
fixes the issue completely.It would be nice if pyw ignored print exceptions or something, since print doesn’t make any difference in context of pyw.
-
repo owner - changed status to invalid
It would be nice if
pyw
ignored print exceptions or something, since print doesn’t make any difference in context ofpyw
.Well, you can still
print()
to files ... anyway, this is not a launcher bug, so marking as Invalid. -
Account Deactivated reporter - changed status to closed
I’ve call pythonw.exe directly and got the same result, so it is bug in CPython2.7.8.
- Log in to comment
This may be because the
print
function behaves differently in the two environments. I suggest wrapping theprint
statement in atry:
/except:
block - what happens then?