VSCode cannot switch matplotlib backend: ImportError: Cannot load backend 'Qt5Agg' which requires the 'qt5' interactive framework

I just want to plot by running Python in VSCode but in turn out failure. I cannot switch the backend from agg to Qt5Agg. However, I can easily do so in the terminal out of VSCode...

The problem in VSCode integrated terminal is shown below, where I have tried various solutions but failed..:

(base) [email protected]:~/test$ export MPLBACKEND=Qt5Agg
(base) [email protected]:~/test$ python
Python 3.8.3 (default, Jul  2 2020, 16:21:59) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> print("Using:",matplotlib.get_backend())
Using: Qt5Agg
>>> import matplotlib.pyplot as plt
>>> plt.plot([1,2,3], [10, 20, 30])
[<matplotlib.lines.Line2D object at 0x7fe26b403d00>]
>>> plt.show()
>>> print("Using:",matplotlib.get_backend())
Using: agg
>>> plt.switch_backend('Qt5Agg')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/zslwyuan-laptop/anaconda3/lib/python3.8/site-packages/matplotlib-3.3.0rc1+627.gff821ba32-py3.8-linux-x86_64.egg/matplotlib/pyplot.py", line 268, in switch_backend
    raise ImportError(
ImportError: Cannot load backend 'Qt5Agg' which requires the 'qt5' interactive framework, as 'headless' is currently running

Any help will be really appreciated. !!

Asked By: yyuanttk

Answer #1:

I had the same problem. Changing "terminal.integrated.inheritEnv" to true in settings solved the problem. I don't know why and have not yet met any side effects.

Answered By: Jiang
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

# More Articles