GUI written in python (with PyQT/PySide2) does not launch

pythonterminalui

I just updated to Big Sur 11.2.1 and wanted to launch a python script that I wrote in a GUI with PySide2. When I launch the script, the terminals seems to be busy with the app:

screenshot1

screenshot2

screenshot3

However nothing more happens: the GUI doe not launch, there is no error message. and I have to kill manually the app. Here is the code

#!/usr/bin/python

# Import PySide classes
import sys
from PySide2.QtWidgets import *

# Create a Qt application
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()

If a run the a script containing only print("hello") from the terminal, this works

Moreover in System Preferences > Security & Privacy > Privacy > Full Disk access, the app iTerm.app is selected (so iTerm.app should have permiession)

Best Answer

I just found the answer in this stackoverflow question. For some weird reason, one has to add the following piece of code at the beginning of the script.

import os
os.environ['QT_MAC_WANTS_LAYER'] = '1'

And that worked for me