VSCode 无法切换 matplotlib 后端: ImportError: 无法加载需要使用交互式框架 'qt5' 的后端 'Qt5Agg'。

3

我想通过在VSCode中运行Python来绘图,但结果失败了。我无法将后端从agg切换到Qt5Agg。但是,在VSCode之外的终端中我可以轻松地这样做...

下面显示了在VSCode集成终端中出现的问题,我已经尝试了各种解决方案,但都失败了:

(base) user@user:~/test$ export MPLBACKEND=Qt5Agg
(base) user@user:~/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
>>> 

非常感谢提供任何帮助!!


尝试使用“pip list”命令检查当前VSCode环境中是否已成功安装了“PyQt5”。我进行了测试,没有出现任何问题。重新加载VSCode后的结果是什么? - Jill Cheng
谢谢Jill,我已经找到了一个在这个帖子中被接受的解决方案!^_^ - yyuanttk
1个回答

9

我曾经遇到过同样的问题。在设置中将"terminal.integrated.inheritEnv"改为true可以解决这个问题。我不知道为什么,也没有遇到任何副作用。


1
谢谢!它有效了!0-0 看起来继承的设置将强制Python使用“agg”后端,以防止弹出窗口。 - yyuanttk
似乎 VS Code 无法捕捉到与终端相同的后端,感谢您的帮助! - Edwardhk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接