Rabu, 13 Maret 2019

py2exe executable doesn't flush stdout properly

I have a python script that prints some 5-digit numbers. I call this script through a Qprocess and read its output which I use later on. On windows the script is packaged into an executable with py2exe and the executable is called.

When I ran either the executable, or the script from a terminal, the output is fine, but when I call the executable through the Qprocess, I get garbage.

The part of my script that prints is like this


print number
sys.stdout.flush()

I tried replacing print with sys.stoud.write(number) and also with sys.stoud.write(number + '\n') but with no success.

my py2exe script is :


setup(
console=['number.py'],
options = {
'py2exe' : {
'bundle_files': 1,
'compressed': True
}
},
zipfile = None
)

I also tried changing console=['number.py'] to windows=['number.py'], also with no success.

I know of -u switch of python, but I need to have it as an executable and I'm also not sure if this will work after all.

I have searched both on SO and google but everyone suggests using sys.stdout.flush() which is not enough for my case.

When I checked the output using a logger, I could see that the size of the output was different with a range from 6 characters to 11 incremented by 1 and then again from 6 to 11 and so on.

I' using Python 2.7 and the Qt is 4.8, I tested on Windows 7 and 8. On Linux everything runs smoothly.

I've wasted almost a whole day on this, I would appreciate any help


I also tried using \r\n when using sys.stoud.write(), but this didn't work too.

Trying to get this to work, I replaced the call to the executable with the call to the python script using the -u switch <python -u myscript.py> and it seems to work fine and the size of the output is always 6. Why is this ?


Looking at the options provided by py2exe, I saw that there is the unbuffered option which says that when True unbuffered stdout and stderr(which I thought is unbuffered by default) are used.

unbuffered: if true, use unbuffered binary stdout and stderr

I set 'unbuffered': True in setup.py and this didn't work either.



from py2exe executable doesn't flush stdout properly

py2exe executable doesn't flush stdout properly Rating: 4.5 Diposkan Oleh: Admin

0 komentar:

Posting Komentar

Popular Posts