Repository navigation
Closed
Description
Bug summary
Running in VS Code interactive mode, %matplotlib qt throws an exception ImportError: Failed to import any of the following Qt binding modules: PyQt6, PySide6, PyQt5, PySide2. However, PySide6 is installed.
Error trace
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
[c:\Users\nhl08\code\oct-invivo-analysis\flatten.py](file:///C:/Users/nhl08/code/oct-invivo-analysis/flatten.py) in line 12
[13](file:///c%3A/Users/nhl08/code/oct-invivo-analysis/flatten.py?line=12) import matplotlib.pyplot as plt
[15](file:///c%3A/Users/nhl08/code/oct-invivo-analysis/flatten.py?line=14) from oct_utils import imshow, imshow2
---> [17](file:///c%3A/Users/nhl08/code/oct-invivo-analysis/flatten.py?line=16) get_ipython().run_line_magic('matplotlib', 'qt')
File [c:\Users\nhl08\miniconda3\envs\sim\lib\site-packages\IPython\core\interactiveshell.py:2414](file:///C:/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py:2414), in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
[2412](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=2411) kwargs['local_ns'] = self.get_local_scope(stack_depth)
[2413](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=2412) with self.builtin_trap:
-> [2414](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=2413) result = fn(*args, **kwargs)
[2416](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=2415) # The code below prevents the output from being displayed
[2417](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=2416) # when using magics with decodator @output_can_be_silenced
[2418](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=2417) # when the last Python token in the expression is a ';'.
[2419](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=2418) if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):
File [c:\Users\nhl08\miniconda3\envs\sim\lib\site-packages\IPython\core\magics\pylab.py:99](file:///C:/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/magics/pylab.py:99), in PylabMagics.matplotlib(self, line)
[97](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/magics/pylab.py?line=96) print("Available matplotlib backends: %s" % backends_list)
[98](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/magics/pylab.py?line=97) else:
---> [99](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/magics/pylab.py?line=98) gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
[100](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/magics/pylab.py?line=99) self._show_matplotlib_backend(args.gui, backend)
File [c:\Users\nhl08\miniconda3\envs\sim\lib\site-packages\IPython\core\interactiveshell.py:3600](file:///C:/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py:3600), in InteractiveShell.enable_matplotlib(self, gui)
[3596](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=3595) print('Warning: Cannot change to a different GUI toolkit: %s.'
[3597](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=3596) ' Using %s instead.' % (gui, self.pylab_gui_select))
[3598](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=3597) gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
-> [3600](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=3599) pt.activate_matplotlib(backend)
[3601](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=3600) configure_inline_support(self, backend)
[3603](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=3602) # Now we must activate the gui pylab wants to use, and fix %run to take
[3604](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/interactiveshell.py?line=3603) # plot updates into account
File [c:\Users\nhl08\miniconda3\envs\sim\lib\site-packages\IPython\core\pylabtools.py:360](file:///C:/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/pylabtools.py:360), in activate_matplotlib(backend)
[355](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/pylabtools.py?line=354) # Due to circular imports, pyplot may be only partially initialised
[356](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/pylabtools.py?line=355) # when this function runs.
[357](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/pylabtools.py?line=356) # So avoid needing matplotlib attribute-lookup to access pyplot.
[358](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/pylabtools.py?line=357) from matplotlib import pyplot as plt
--> [360](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/pylabtools.py?line=359) plt.switch_backend(backend)
[362](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/pylabtools.py?line=361) plt.show._needmain = False
[363](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/pylabtools.py?line=362) # We need to detect at runtime whether show() is called by the user.
[364](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/IPython/core/pylabtools.py?line=363) # For this, we wrap it into a decorator which adds a 'called' flag.
File [c:\Users\nhl08\miniconda3\envs\sim\lib\site-packages\matplotlib\pyplot.py:271](file:///C:/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/pyplot.py:271), in switch_backend(newbackend)
[268](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/pyplot.py?line=267) # have to escape the switch on access logic
[269](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/pyplot.py?line=268) old_backend = dict.__getitem__(rcParams, 'backend')
--> [271](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/pyplot.py?line=270) backend_mod = importlib.import_module(
[272](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/pyplot.py?line=271) cbook._backend_module_name(newbackend))
[274](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/pyplot.py?line=273) required_framework = _get_required_interactive_framework(backend_mod)
[275](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/pyplot.py?line=274) if required_framework is not None:
File [c:\Users\nhl08\miniconda3\envs\sim\lib\importlib\__init__.py:126](file:///C:/Users/nhl08/miniconda3/envs/sim/lib/importlib/__init__.py:126), in import_module(name, package)
[124](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/importlib/__init__.py?line=123) break
[125](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/importlib/__init__.py?line=124) level += 1
--> [126](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/importlib/__init__.py?line=125) return _bootstrap._gcd_import(name[level:], package, level)
File :1050, in _gcd_import(name, package, level)
File :1027, in _find_and_load(name, import_)
File :1006, in _find_and_load_unlocked(name, import_)
File :688, in _load_unlocked(spec)
File :883, in exec_module(self, module)
File :241, in _call_with_frames_removed(f, *args, **kwds)
File [c:\Users\nhl08\miniconda3\envs\sim\lib\site-packages\matplotlib\backends\backend_qt5agg.py:7](file:///C:/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qt5agg.py:7)
[4](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qt5agg.py?line=3) from .. import backends
[6](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qt5agg.py?line=5) backends._QT_FORCE_QT5_BINDING = True
----> [7](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qt5agg.py?line=6) from .backend_qtagg import ( # noqa: F401, E402 # pylint: disable=W0611
[8](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qt5agg.py?line=7) _BackendQTAgg, FigureCanvasQTAgg, FigureManagerQT, NavigationToolbar2QT,
[9](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qt5agg.py?line=8) FigureCanvasAgg, FigureCanvasQT)
[12](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qt5agg.py?line=11) @_BackendQTAgg.export
[13](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qt5agg.py?line=12) class _BackendQT5Agg(_BackendQTAgg):
[14](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qt5agg.py?line=13) pass
File [c:\Users\nhl08\miniconda3\envs\sim\lib\site-packages\matplotlib\backends\backend_qtagg.py:9](file:///C:/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qtagg.py:9)
[5](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qtagg.py?line=4) import ctypes
[7](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qtagg.py?line=6) from matplotlib.transforms import Bbox
----> [9](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qtagg.py?line=8) from .qt_compat import QT_API, _enum
[10](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qtagg.py?line=9) from .backend_agg import FigureCanvasAgg
[11](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/backend_qtagg.py?line=10) from .backend_qt import QtCore, QtGui, _BackendQT, FigureCanvasQT
File [c:\Users\nhl08\miniconda3\envs\sim\lib\site-packages\matplotlib\backends\qt_compat.py:135](file:///C:/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/qt_compat.py:135)
[133](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/qt_compat.py?line=132) break
[134](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/qt_compat.py?line=133) else:
--> [135](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/qt_compat.py?line=134) raise ImportError(
[136](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/qt_compat.py?line=135) "Failed to import any of the following Qt binding modules: {}"
[137](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/qt_compat.py?line=136) .format(", ".join(_ETS.values())))
[138](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/qt_compat.py?line=137) else: # We should not get there.
[139](file:///c%3A/Users/nhl08/miniconda3/envs/sim/lib/site-packages/matplotlib/backends/qt_compat.py?line=138) raise AssertionError(f"Unexpected QT_API: {QT_API}")
ImportError: Failed to import any of the following Qt binding modules: PyQt6, PySide6, PyQt5, PySide2
Code for reproduction
Run in a VS Code interactive cell
# %%
import matplotlib.pyplot as plt
%matplotlib qtAdditional 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
Activity
tacaswell commentedon Apr 13, 2023
Are you sure that pyside6 is installed in the environment that is being used for the terminal.
kwsp commentedon Apr 13, 2023
Yes. In the same interactive shell, I can run the following cell
kwsp commentedon Apr 13, 2023
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
%matplotlib qtdoes this happen.tacaswell commentedon Apr 13, 2023
If you import
pyside6before you do%matplotlib qtdoes it work?kwsp commentedon Apr 13, 2023
It does not unfortunately. Just tested in a fresh shell:
I also want to add that this has been happening to me for many months, on two separate machines (Windows 10 and Windows 11).
tacaswell commentedon Apr 13, 2023
Can you try the code in
matplotlib/lib/matplotlib/backends/qt_compat.py
Lines 82 to 89 in bff4681
does
%gui qtwork?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.
kwsp commentedon Apr 13, 2023
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
inline.6.5.0
It's not using the PySide6 backend:
[<class 'matplotlib.backends.backend_agg.FigureCanvasAgg'>, <class 'matplotlib.backend_bases.FigureCanvasBase'>, <class 'object'>]No I understand. Thanks for your help. I'm an absolute noob with Windows but I need to develop native Windows apps for work :(
tacaswell commentedon Apr 14, 2023
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?
kwsp commentedon Apr 24, 2023
plt.switch_backend('qtagg')gave the same error:ImportError: Failed to import any of the following Qt binding modules: PyQt6, PySide6, PyQt5, PySide2In the same VS Code interactive shell, I can run the following hello world program with PySide6 and a Qt window pops up.
This actually isn't specific to Windows and the above results are from an Ubuntu 22.04 machine.
kwsp commentedon Apr 24, 2023
The reason I'm using conda + pip:
pyproject.tomlfile. I can then easily install my packages withpip install .and havepipmanage the dependencies for me. I have yet to find a painless way to manage my own packages withconda.That said, in this case, I'm exclusively using
pipto manage packages in this environment and only usingcondaas the python version/virtual environment manager, which I heard is innocuous.Re: mixing conda env and pip - I just took a look, even
matplotlib(when usingcondavirtual environments) needs to usepip install -e .to installmatplotlib...: https://matplotlib.org/devdocs/devel/development_setup.html#create-a-dedicated-environmenttacaswell commentedon Apr 24, 2023
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
qt_compat.pycan you sort out exactly what is failing when we try each of the bindings?kwsp commentedon Apr 25, 2023
I looked into
qt_compat.py, and when I executeI get the same exception.
However, I just found a fix! Since I know I'm using
PySide6, I can run this linematplotlib/lib/matplotlib/backends/qt_compat.py
Line 83 in bff4681
PySide6has always been installed. Once I do that, the above imports ofqt_compat._setup_pyqt5plus()works and%matplotlib qtworks.kwsp commentedon Apr 25, 2023
Ok I stepped through
qt_compat.pywith a debugger and found the issue.When I run
%matplotlib qt, mpl loadsqt_compat.pywhich in turn executes the file.matplotlib/lib/matplotlib/backends/qt_compat.py
Lines 39 to 46 in bff4681
After this,
QT_APIremains undefined because no Qt bindings have been imported yet. So, we fall through to this case:matplotlib/lib/matplotlib/backends/qt_compat.py
Lines 47 to 59 in bff4681
For my environment,
mpl.rcParams._get_backend_or_none()returnsQt5Agg, even though I have no Qt5 bindings installed. Still, we run into this casematplotlib/lib/matplotlib/backends/qt_compat.py
Line 56 in bff4681
So now,
QT_API = Noneand_QT_FORCE_QT5_BINDING = True, and we fall though to this case while_setup_qt5plus()is never executed.matplotlib/lib/matplotlib/backends/qt_compat.py
Line 115 in bff4681
Obviously, we now get to this line since no Qt5 bindings are installed.
matplotlib/lib/matplotlib/backends/qt_compat.py
Line 134 in bff4681
My work around of importing
PySide6.QtCoremanually above basically negates this issue because once Qt6 bindings are insys.modules,qt_compat.pywould setQT_API = QT_API_PYSIDE6.kwsp commentedon Apr 25, 2023
On a first pass look, it seems for me this is caused by
matplotlib/lib/matplotlib/backends/qt_compat.py
Line 56 in bff4681
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
Qt5Aggin my environment when I only haveQt6bindings installed the problem? Am not familiar withAggbackends and what they mean so would love your inputtacaswell commentedon Apr 25, 2023
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
'qt5agg'? Could be anmatplotlibrcor ampl.usein a start up script.The bug to fix here is that if we are restricting to Qt5 then the error message should not list the Qt6 bindings!
kwsp commentedon Apr 25, 2023
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
Qt5Aggbackend by default. I just tested again in a clean conda environment on Windows 10, with justmatplotlibandPySide6installed, and trying%matplotlib qtstill gives me that exception. Here's my pip listtacaswell commentedon Apr 25, 2023
It should not,
matplotlib/lib/matplotlib/pyplot.py
Lines 233 to 267 in b3bd929
'qt5agg'.kwsp commentedon Apr 26, 2023
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
kwsp commentedon Apr 26, 2023
I set a breakpoint on line 234 here, stepped through the code that raised the
ImportError, and confirmed this branch was never hit.kwsp commentedon Apr 26, 2023
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
%matplotlib qt,qtalways maps toQt5Agg. So if I only have Qt6 bindings, this would always break because IPython tells matplotlibto useQt5Aggfirst, before matplotlib tries to actually import bindings.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
ksunden commentedon Apr 26, 2023
(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
%matplotlib qt6instead... longer term, perhaps IPython should update that mapping.Regardless, going to close as this is not a change we can do, as far as I can tell
ksunden commentedon Apr 26, 2023
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.