Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Bug]: VSCode matplotlib interactive mode cannot import Qt bindings #25673

Copy link
Copy link
Closed
@kwsp

Description

@kwsp
Issue body actions

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 qt

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.