12

It is always risky to upgrade your operation system. It is likely you will encounter some compatibility issue. I took the risk to upgrade my macOS from Catalina to the newest Big Sur. After that, the display in the new OS looks pretty, but all my PyQt5 apps could not be launched in this new OS. The GUI window does not pop up as usual, and there is no error message showing in the terminal. I spent the whole day trying to figure out what makes this problem. I found the solution but in a weird way which I feel confused.

It turns out that the apps comes back to normal after I add the following three lines in the main script.

import matplotlib
import matplotlib.pyplot as plt

matplotlib.use('TkAgg')

It seems to me the new OS has some compatibility issue with Qt5Agg back-end. But the strange thing is that this solution also works for one of the Pyqt5 app, where I don't use matplotlib at all.

The Python version I used is 3.8.4, and the PyQt5 version I have is 5.15.1.

I hope somebody could explain to me what happen under the hood that makes this solution work. Also I hope this temporary solution can help somebody with the same problem.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Canrong Qiu
  • 136
  • 1
  • 6

7 Answers7

13

A reply to the PyQt mailing list pointed out that setting this env var works:

QT_MAC_WANTS_LAYER=1

Found via Is there any solution regarding to PyQt library doesn't work in Mac OS Big Sur? and https://forums.macrumors.com/threads/pyqt5-and-big-sur.2260773/?post=29243620#post-29243620

Eric
  • 180
  • 8
  • wow, this is a great find! I was fighting this for a while running python 3.6 using pyside2, this worked for me. – Michael Jan 23 '21 at 04:54
4

I can confirm that matplotlib.use('TkAgg') followed by matplotlib.use('Qt5Agg') makes things work for me, too. I whittled it down to this as also working:

# from matplotlib.backends import _tkagg
import _tkinter
import matplotlib.pyplot as plt
plt.figure()

So it's something about the compiled _tkinter module. Maybe the inputhook?

Eric
  • 180
  • 8
  • Stumble on the same issue myself with our PyQt5 app adding the import _tkinter to file containing the QApplication seem to cause the widgets to appear – Duncan Napier Nov 17 '20 at 12:04
3

As @Eric said, just add the following on the very start of your code, before the PySide2 import:

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

Then import PyQt5/PySide2.

Martí Climent
  • 407
  • 4
  • 9
1

I followed the solution here and downgraded to PyQt 5.13. This solved my issue and allowed my compiled apps to run on Big Sur.

pip install PyQt5==5.13
0

for me the suggested solution brought a crashes on a breackpoints in pycharm ... the only thing helped : https://forums.macrumors.com/threads/pyqt5-and-big-sur.2260773/ all worked as a magic ... hope QT will fix it soon

Kamornik Cola
  • 644
  • 8
  • 20
0

I am using macOS Big Sur Version 11.2.2

As suggested by Eric, enter the following line in the terminal before launching your application:

export QT_MAC_WANTS_LAYER=1

This fixed the issue for me!

Tun Kapgen
  • 81
  • 3
0

Adding this to my python program worked for me

import os
os.environ["QT_MAC_WANTS_LAYER"] = "1"
m4n0
  • 29,823
  • 27
  • 76
  • 89