-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: VSCode matplotlib interactive mode cannot import Qt bindings #25673
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
Comments
Are you sure that pyside6 is installed in the environment that is being used for the terminal. |
Yes. In the same interactive shell, I can run the following cell # %%
import PySide6
print(PySide6.__version__) # Prints 6.5.0 |
When I start python from the command line and try to plot things, interactive plots work. It's only in VS Code interactive mode, when I try to enable interactive plots with |
If you import |
It does not unfortunately. Just tested in a fresh shell: # %%
import PySide6
import matplotlib.pyplot as plt
%matplotlib qt
I also want to add that this has been happening to me for many months, on two separate machines (Windows 10 and Windows 11). |
Can you try the code in matplotlib/lib/matplotlib/backends/qt_compat.py Lines 82 to 89 in bff4681
does Does
work? What version of pyside6? In the shells where it works can you check
to make sure it really is using pyside6 in those cases. Sorry for asking many questions, I do not have a window system set up to reproduce this. |
This works if I run directly in the shell.
This line runs without exception, but matplotlib would still use the Agg backend.
This executes without exception, but the result plot is still
6.5.0
It's not using the PySide6 backend:
No I understand. Thanks for your help. I'm an absolute noob with Windows but I need to develop native Windows apps for work :( |
Can you get a Pyside6 "hello world" app to work in the vscode terminal? Another thing I just noticed is that there is "conda" in your paths, but you said you installed via pip. Try making a fresh environment and installing everything from conda. Mixing conda and wheels can go bad in odd ways (see https://pypackaging-native.github.io). Can you try older versions of pyside? If you use
early does that make any difference? |
In the same VS Code interactive shell, I can run the following hello world program with PySide6 and a Qt window pops up. from PySide6 import QtWidgets
app = QtWidgets.QApplication()
win = QtWidgets.QWidget()
win.show()
app.exec() This actually isn't specific to Windows and the above results are from an Ubuntu 22.04 machine. |
The reason I'm using conda + pip:
That said, in this case, I'm exclusively using Re: mixing conda env and pip - I just took a look, even |
I can not reproduce this (I got the interactive window by right click -> "run in interactive window" on an empty file and selected the system Python (which on my system has enough of the stack installed) for the Python that vscode is using). Looking at the code in |
I looked into import matplotlib.backends.qt_compat
matplotlib.backends.qt_compat._setup_pyqt5plus() I get the same exception. However, I just found a fix! Since I know I'm using
PySide6 has always been installed. Once I do that, the above imports of qt_compat._setup_pyqt5plus() works and %matplotlib qt works.
from PySide6 import QtCore, QtGui, QtWidgets, __version__
import shiboken6 |
Ok I stepped through When I run matplotlib/lib/matplotlib/backends/qt_compat.py Lines 39 to 46 in bff4681
After this, matplotlib/lib/matplotlib/backends/qt_compat.py Lines 47 to 59 in bff4681
For my environment,
So now,
Obviously, we now get to this line since no Qt5 bindings are installed.
My work around of importing |
On a first pass look, it seems for me this is caused by
which if I check git blame, you committed 2bc0c1c So the question is: is this line of forcing Qt5 causing this bug, or is it the fact that MPL is using |
This says something in your system is setting the backend to 'qt5agg' which we take to mean "I want to us Qt5" (see #22005 and the linked issuse). Is there something in your environment forcing the backend to The bug to fix here is that if we are restricting to Qt5 then the error message should not list the Qt6 bindings! |
I doubt its an environment issue, since I can reproduce this problem on 3 separate machines (Windows 10, Windows 11, Ubuntu 22.04) with a clean conda environment. Could you point me to how mpl selects the default backend? I'm curious to see where in code MPL decides to use the
|
It should not, matplotlib/lib/matplotlib/pyplot.py Lines 233 to 267 in b3bd929
'qt5agg' .
|
Here's are the exact steps to reproduce on Windows 10 and Windows 11. 1. Create a clean Conda environment and install deps
2. In VS Code interactive, run the following cells step by step# %%
import matplotlib.pyplot as plt
import matplotlib as mpl
# %%
mpl.get_backend() # returns 'module://matplotlib_inline.backend_inline'
# %%
plt.plot(range(10)) # Plots inline OK
# %%
mpl.get_backend() # Still returns 'module://matplotlib_inline.backend_inline'
# %%
%matplotlib qt
plt.plot(range(10)) # ImportError: Failed to import any of the following Qt binding modules...
# %%
mpl.get_backend() # returns 'Qt5Agg' |
I set a breakpoint on line 234 here, stepped through the code that raised the |
Ok what the hell. The problem isn't with matplotlib, but IPython. I hardcore stepped through the code this time, and it turns out if you do https://github.com/ipython/ipython/blob/main/IPython/core/pylabtools.py#L26 https://github.com/ipython/ipython/blob/main/IPython/core/pylabtools.py#L301-L322 |
(I was about to post the same link, in fact just for posterity, I'll post the permalink rather than the main branch which can change: https://github.com/ipython/ipython/blob/396593e7ad8cab3a9c36fb0f3e26cbf79cff069c/IPython/core/pylabtools.py#L26) Short term, you should be resolved by doing Regardless, going to close as this is not a change we can do, as far as I can tell |
Thought a little bit more and opened #25772, which would at least have helped narrow in on this, by not purporting to have tried qt6 when it does not actually do so. |
Bug summary
Running in VS Code interactive mode,
%matplotlib qt
throws an exceptionImportError: Failed to import any of the following Qt binding modules: PyQt6, PySide6, PyQt5, PySide2
. However, PySide6 is installed.Error trace
Code for reproduction
Run in a VS Code interactive cell
Additional information
ipykernel: 6.22.0
ipython: 8.12.0
ipywidgets: 8.0.6
Operating system
Windows 10 and Windows 11 (2 separate computers)
Matplotlib Version
3.7.1
Matplotlib Backend
Qt5Agg
Python version
3.10.10
Jupyter version
No response
Installation
pip
The text was updated successfully, but these errors were encountered: