aviyaChe
aviyaChe

Reputation: 155

Redirecting stdout of a simple PyQt5 application

I have the following super simple qt application:

import sys
from PyQt5 import QtWidgets

print('hello \n')
app = QtWidgets.QApplication(sys.argv)
app.exec_()

When executing it normally with terminal hello message is shown, h/w when redirecting stdout to a file:

python3 qt_redirect.py > file.txt

the file remains empty.

Help me guys, what am I missing here?

python version - 3.6.9 PyQt5 version - 5.14.1 os - ubuntu 18.04.3 LTS

Upvotes: 2

Views: 337

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

The solution is to clean the buffer when printing and for this there are several options:

  • flush=True
print('hello \n'. flush=True)
  • Set the PYTHONUNBUFFERED environment variable to "1":
PYTHONUNBUFFERED=1 python3 qt_redirect.py > file.txt
  • User the python "-u" option:
python3 -u  qt_redirect.py > file.txt

Upvotes: 3

Related Questions