<h3 dir="auto">Bug summary</h3>
<p dir="auto"><code class="notranslate">matplotlib.get_backend()</code> fails with the indicated exception. This method is also executed when the debugger hits a breakpoint, even if not actually using matplotlib. A fix/workaround is included.</p>
<h3 dir="auto">Code for reproduction</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import unittest
class Test(unittest.TestCase):
    def test(self):   # Debug this test.
        pass          # Place a breakpoint here.


# Alternatively, without a unit test:
import matplotlib
print(matplotlib.get_backend())"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">unittest</span>
<span class="pl-k">class</span> <span class="pl-v">Test</span>(<span class="pl-s1">unittest</span>.<span class="pl-v">TestCase</span>):
    <span class="pl-k">def</span> <span class="pl-en">test</span>(<span class="pl-s1">self</span>):   <span class="pl-c"># Debug this test.</span>
        <span class="pl-k">pass</span>          <span class="pl-c"># Place a breakpoint here.</span>


<span class="pl-c"># Alternatively, without a unit test:</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>
<span class="pl-en">print</span>(<span class="pl-s1">matplotlib</span>.<span class="pl-en">get_backend</span>())</pre></div>
<h3 dir="auto">Actual outcome</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="./tests/test_test.py::Test::test Failed with Error: int() argument must be a string, a bytes-like object or a real number, not 'KeyboardModifier'
  File &quot;C:\Program Files\Python310\lib\unittest\case.py&quot;, line 59, in testPartExecutor
    yield
  File &quot;C:\Program Files\Python310\lib\unittest\case.py&quot;, line 591, in run
    self._callTestMethod(testMethod)
  File &quot;C:\Program Files\Python310\lib\unittest\case.py&quot;, line 549, in _callTestMethod
    method()
  File &quot;D:\Lasertrimmer\Software\ltsoftware\tests\test_test.py&quot;, line 4, in test
    pass
  File &quot;D:\Lasertrimmer\Software\ltsoftware\tests\test_test.py&quot;, line 4, in test
    pass
  File &quot;c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py&quot;, line 987, in trace_dispatch
    self.do_wait_suspend(thread, frame, event, arg)
  File &quot;c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py&quot;, line 164, in do_wait_suspend
    self._args[0].do_wait_suspend(*args, **kwargs)
  File &quot;c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py&quot;, line 2062, in do_wait_suspend
    keep_suspended = self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread, frames_tracker)
  File &quot;c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py&quot;, line 2085, in _do_wait_suspend
    self._activate_gui_if_needed()
  File &quot;c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py&quot;, line 1581, in _activate_gui_if_needed
    activate_function()
  File &quot;c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py&quot;, line 1565, in &lt;lambda&gt;
    self.mpl_modules_for_patching = {&quot;matplotlib&quot;: lambda: activate_matplotlib(do_enable_gui),
  File &quot;c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydev_ipython\matplotlibtools.py&quot;, line 93, in activate_matplotlib
    gui, backend = find_gui_and_backend()
  File &quot;c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydev_ipython\matplotlibtools.py&quot;, line 45, in find_gui_and_backend
    backend = matplotlib.rcParams['backend']
  File &quot;C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\__init__.py&quot;, line 677, in __getitem__
    plt.switch_backend(rcsetup._auto_backend_sentinel)
  File &quot;C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\pyplot.py&quot;, line 252, in switch_backend
    switch_backend(candidate)
  File &quot;C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\pyplot.py&quot;, line 265, in switch_backend
    backend_mod = importlib.import_module(
  File &quot;C:\Program Files\Python310\lib\importlib\__init__.py&quot;, line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1050, in _gcd_import
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1027, in _find_and_load
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1006, in _find_and_load_unlocked
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 688, in _load_unlocked
  File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 883, in exec_module
  File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 241, in _call_with_frames_removed
  File &quot;C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\backends\backend_qtagg.py&quot;, line 12, in &lt;module&gt;
    from .backend_qt import QtCore, QtGui, _BackendQT, FigureCanvasQT
  File &quot;C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\backends\backend_qt.py&quot;, line 72, in &lt;module&gt;
    _MODIFIER_KEYS = [
  File &quot;C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\backends\backend_qt.py&quot;, line 73, in &lt;listcomp&gt;
    (_to_int(getattr(_enum(&quot;QtCore.Qt.KeyboardModifier&quot;), mod)),
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'KeyboardModifier'"><pre class="notranslate"><code class="notranslate">./tests/test_test.py::Test::test Failed with Error: int() argument must be a string, a bytes-like object or a real number, not 'KeyboardModifier'
  File "C:\Program Files\Python310\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "C:\Program Files\Python310\lib\unittest\case.py", line 591, in run
    self._callTestMethod(testMethod)
  File "C:\Program Files\Python310\lib\unittest\case.py", line 549, in _callTestMethod
    method()
  File "D:\Lasertrimmer\Software\ltsoftware\tests\test_test.py", line 4, in test
    pass
  File "D:\Lasertrimmer\Software\ltsoftware\tests\test_test.py", line 4, in test
    pass
  File "c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py", line 987, in trace_dispatch
    self.do_wait_suspend(thread, frame, event, arg)
  File "c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_frame.py", line 164, in do_wait_suspend
    self._args[0].do_wait_suspend(*args, **kwargs)
  File "c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py", line 2062, in do_wait_suspend
    keep_suspended = self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread, frames_tracker)
  File "c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py", line 2085, in _do_wait_suspend
    self._activate_gui_if_needed()
  File "c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py", line 1581, in _activate_gui_if_needed
    activate_function()
  File "c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py", line 1565, in &lt;lambda&gt;
    self.mpl_modules_for_patching = {"matplotlib": lambda: activate_matplotlib(do_enable_gui),
  File "c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydev_ipython\matplotlibtools.py", line 93, in activate_matplotlib
    gui, backend = find_gui_and_backend()
  File "c:\Users\laboreng\.vscode\extensions\ms-python.python-2022.16.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydev_ipython\matplotlibtools.py", line 45, in find_gui_and_backend
    backend = matplotlib.rcParams['backend']
  File "C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\__init__.py", line 677, in __getitem__
    plt.switch_backend(rcsetup._auto_backend_sentinel)
  File "C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\pyplot.py", line 252, in switch_backend
    switch_backend(candidate)
  File "C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\pyplot.py", line 265, in switch_backend
    backend_mod = importlib.import_module(
  File "C:\Program Files\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "&lt;frozen importlib._bootstrap&gt;", line 1050, in _gcd_import
  File "&lt;frozen importlib._bootstrap&gt;", line 1027, in _find_and_load
  File "&lt;frozen importlib._bootstrap&gt;", line 1006, in _find_and_load_unlocked
  File "&lt;frozen importlib._bootstrap&gt;", line 688, in _load_unlocked
  File "&lt;frozen importlib._bootstrap_external&gt;", line 883, in exec_module
  File "&lt;frozen importlib._bootstrap&gt;", line 241, in _call_with_frames_removed
  File "C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\backends\backend_qtagg.py", line 12, in &lt;module&gt;
    from .backend_qt import QtCore, QtGui, _BackendQT, FigureCanvasQT
  File "C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\backends\backend_qt.py", line 72, in &lt;module&gt;
    _MODIFIER_KEYS = [
  File "C:\Users\laboreng\AppData\Roaming\Python\Python310\site-packages\matplotlib\backends\backend_qt.py", line 73, in &lt;listcomp&gt;
    (_to_int(getattr(_enum("QtCore.Qt.KeyboardModifier"), mod)),
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'KeyboardModifier'
</code></pre></div>
<h3 dir="auto">Expected outcome</h3>
<p dir="auto">VSCode should stay at the breakpoint without raising an exception. The alternative code should print <code class="notranslate">QtAgg</code> without raising an exception.</p>
<h3 dir="auto">Additional information</h3>
<ul dir="auto">
<li>Just running (not debugging) the code does not cause the problem.</li>
<li>Debugging the code when no breakpoint is hit does not cause the problem.</li>
<li><code class="notranslate">matplotlib.get_backend()</code> is called automatically when the debugger hits a breakpoint (even when not actually using matplotlib in the code), which causes the exception.</li>
<li>The issue is probably not triggered if
<ul dir="auto">
<li>explicitly using another backend than PySide6</li>
<li>PyQt6 is installed as well</li>
</ul>
</li>
</ul>
<p dir="auto">On my system, I was able to fix the behaviour as follows:</p>
<div class="highlight highlight-source-diff notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="--- &quot;a/C:\\Users\\laboreng\\AppData\\Roaming\\Python\\Python310\\site-packages\\matplotlib\\backends\\qt_compat.py&quot;
+++ &quot;b/C:\\Users\\laboreng\\AppData\\Roaming\\Python\\Python310\\site-packages\\matplotlib\\backends\\qt_compat_fix.py&quot;
@@ -141,7 +141,7 @@ if (sys.platform == 'darwin' and
 # PyQt6 enum compat helpers.


-_to_int = operator.attrgetter(&quot;value&quot;) if QT_API == &quot;PyQt6&quot; else int
+_to_int = operator.attrgetter(&quot;value&quot;) if QT_API in [&quot;PyQt6&quot;, &quot;PySide6&quot;] else int


 @functools.lru_cache(None)"><pre class="notranslate"><span class="pl-md">--- "a/C:\\Users\\laboreng\\AppData\\Roaming\\Python\\Python310\\site-packages\\matplotlib\\backends\\qt_compat.py"</span>
<span class="pl-mi1">+++ "b/C:\\Users\\laboreng\\AppData\\Roaming\\Python\\Python310\\site-packages\\matplotlib\\backends\\qt_compat_fix.py"</span>
<span class="pl-mdr">@@ -141,7 +141,7 @@</span> if (sys.platform == 'darwin' and
 # PyQt6 enum compat helpers.


<span class="pl-md"><span class="pl-md">-</span>_to_int = operator.attrgetter("value") if QT_API == "PyQt6" else int</span>
<span class="pl-mi1"><span class="pl-mi1">+</span>_to_int = operator.attrgetter("value") if QT_API in ["PyQt6", "PySide6"] else int</span>


 @functools.lru_cache(None)</pre></div>
<h3 dir="auto">Operating system</h3>
<p dir="auto">Windows 11</p>
<h3 dir="auto">Matplotlib Version</h3>
<p dir="auto">3.6.1</p>
<h3 dir="auto">Matplotlib Backend</h3>
<p dir="auto">QtAgg (code fails without the indicated fix)</p>
<h3 dir="auto">Python version</h3>
<p dir="auto">Python 3.10.8</p>
<h3 dir="auto">Jupyter version</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Installation</h3>
<p dir="auto">pip</p>
<h3 dir="auto">Bug summary</h3>
<p dir="auto">Error while importing matplotlib qt backend.</p>
<p dir="auto">The version details are as below<br>
matplotlib: 3.6.1<br>
pyside: 6.4.0<br>
python: 3.9.9</p>
<p dir="auto">on a macOS-M1 machine.</p>
<h3 dir="auto">Code for reproduction</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from matplotlib.backends.backend_qtagg import NavigationToolbar2QT as NavigationToolbar"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">backends</span>.<span class="pl-s1">backend_qtagg</span> <span class="pl-k">import</span> <span class="pl-v">NavigationToolbar2QT</span> <span class="pl-k">as</span> <span class="pl-v">NavigationToolbar</span></pre></div>
<h3 dir="auto">Actual outcome</h3>
<p dir="auto">Traceback (most recent call last):<br>
File "", line 1, in <br>
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/backends/backend_qtagg.py", line 12, in <br>
from .backend_qt import QtCore, QtGui, _BackendQT, FigureCanvasQT<br>
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/backends/backend_qt.py", line 72, in <br>
_MODIFIER_KEYS = [<br>
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/backends/backend_qt.py", line 73, in <br>
(_to_int(getattr(_enum("QtCore.Qt.KeyboardModifier"), mod)),<br>
TypeError: int() argument must be a string, a bytes-like object or a number, not 'KeyboardModifier'</p>
<h3 dir="auto">Expected outcome</h3>
<p dir="auto">No error!</p>
<h3 dir="auto">Additional information</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Operating system</h3>
<p dir="auto">OS/X</p>
<h3 dir="auto">Matplotlib Version</h3>
<p dir="auto">3.6.1</p>
<h3 dir="auto">Matplotlib Backend</h3>
<p dir="auto">MacOSX</p>
<h3 dir="auto">Python version</h3>
<p dir="auto">3.9.9</p>
<h3 dir="auto">Jupyter version</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Installation</h3>
<p dir="auto">pip</p>