Skip to content

BUG: Qt5Agg / PyQt5 not compatible with macOS Big Sur #18954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
larsoner opened this issue Nov 15, 2020 · 13 comments
Closed

BUG: Qt5Agg / PyQt5 not compatible with macOS Big Sur #18954

larsoner opened this issue Nov 15, 2020 · 13 comments

Comments

@larsoner
Copy link
Contributor

larsoner commented Nov 15, 2020

I have Qt5Agg set as my default backend. I recently upgraded my macOS system to Big Sur, and now import matplotlib.pyplot as plt; plt.figure() ends up hanging the interpreter. It seems like this is really a PyQt5 problem, but I thought I'd post some sort of information here as a bit of a warning for people, and as a place for people to mention workarounds if they have them. Or maybe I'm the only one with this problem, in which case sorry for the noise! FWIW I tried PyQt5 5.12 and 5.15.1, same problem on both. I also tried pyside2, it at least does not hang but no window is shown.

In the meantime I'll probably set my backend to MacOSX.

EDIT: My matplotlibrc specifies both backend: Qt5Agg and interactive: True

@larsoner
Copy link
Contributor Author

I assume that this is the upstream issue:

https://bugreports.qt.io/browse/QTBUG-85546

I also posted to the PyQt5 mailing list about this. I'll update this if I get more information.

@larsoner
Copy link
Contributor Author

larsoner commented Nov 15, 2020

I noticed that there is a StackOverflow post about this:

https://stackoverflow.com/questions/64833558/apps-not-popping-up-on-macos-big-sur-11-0-1/64847505#64847505

After some investigation, it looks like importing matplotlib.backends._tkagg for some reason fixes things:

from matplotlib.backends import _tkagg
import matplotlib.pyplot as plt
fig = plt.figure()
assert 'QTAgg' in fig.canvas.__class__.__name__

It looks like that's a compiled module, I'll see if it sets some system-level stuff to make itself work. Maybe that's what we need to do for PyQt5 on Big Sur...

@larsoner
Copy link
Contributor Author

larsoner commented Nov 15, 2020

Actually importing tkinter or _tkinter (the C .so that import tkinter imports) is enough:

import _tkinter
import matplotlib.pyplot as plt
fig = plt.figure()
assert 'QTAgg' in fig.canvas.__class__.__name__

So I guess there is some magic in here?

https://github.com/python/cpython/blob/a26215db11cfcf7b5f55cab9e91396761a0e0bcf/Modules/_tkinter.c#L3465-L3646

EDIT: I'm guessing it's the inputhook or TCL code somehow, but this is too low-level for me personally to know.

@larsoner larsoner changed the title BUG: Qt5Agg / PyQt5 not compatible with BigSur BUG: Qt5Agg / PyQt5 not compatible with macOS Big Sur Nov 15, 2020
@tacaswell
Copy link
Member

Based on seeing in one of the linked issues that the SDK will give conflicting answers based on who it thinks is asking. My guess is that loading tk forces the SDK in to "old" mode which gives back qt an answer that it likes. If that is the case so level of PRE_LOAD would also "fix" the problem.

@larsoner
Copy link
Contributor Author

@tacaswell
Copy link
Member

Given that this seems to be generic to pyqt, I don't think we have to do anything about this right now, but leaving open as a signpost.

@larsoner
Copy link
Contributor Author

I was also told on the PyQt5 mailing list that 5.15.2 will have this QT_MAC_WANTS_LAYER on by default. But that won't help people with previous versions...

I don't think we have to do anything about this right now, but leaving open as a signpost.

Agreed it's not really matplotlib's problem. However, one option to try to save people some pain would be to add a platform.mac_ver() + os.getenv('QT_MAC_WANTS_LAYER') check in the PyQt5 backend, and warn people. This seems like an easy backport to the current maintained branches. The only real downside I see (other than it adding a handful of lines and the associated, hopefully low maintenance burden) is that this runs the risk of warning people who don't need to be warned if it turns out not to be a universal problem. To me the benefit it would bring to people who have their interpreter inexplicably hang would be worth this, but that's easy for me to say since I wouldn't be the one maintaining the new mpl lines :)

@dstansby
Copy link
Member

I'm struggling to reproduce this issue. I have these versions installed:

# Name                    Version                   Build  Channel
pyqt                      5.9.2            py39h23ab428_6  
qt                        5.9.7                h468cd18_1  

and the following script is working fine for me, on macos 11.1:

import matplotlib
matplotlib.use('QT5Agg')
import matplotlib.pyplot as plt

plt.figure()
plt.show()

and just to be sure, I've checked that echo $QT_MAC_WANTS_LAYER doesn't print anything. Any ideas if there's something else I need to be doing to reproduce the bug?

@larsoner
Copy link
Contributor Author

larsoner commented Jan 22, 2021

I never went as far back as 5.9 in the Qt versions. Doing some installs locally it looks like there is no problem on 5.11.3 (as far back as I could go with my Python 3.8 install using pip) but on 5.12.3 or above I can reproduce the issue with your snippet.

@tacaswell
Copy link
Member

The conda packaged versions of Qt are getting painfully old these days...

@dstansby
Copy link
Member

dstansby commented Jan 22, 2021

😆 well I seem to get a different error with 5.12.2 (will open a new issue), but I can confirm this with 5.15.1

@dstansby
Copy link
Member

Huh, now I've re-installed 5.12.2 it seems to be working fine.

@dstansby
Copy link
Member

dstansby commented Apr 5, 2021

I think this is closed by #19334? If anyone disagrees please leave a comment or re-open.

@dstansby dstansby closed this as completed Apr 5, 2021
@tacaswell tacaswell added this to the v3.4.0 milestone Apr 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants