在 PyCharm 中,当我尝试使用其交互式控制台绘制某些内容时,例如:
In[2]: from matplotlib.pyplot import *
In[3]: x = range(5)
In[4]: y = range(5,10)
In[5]: plot(x,y)
WARNING: QApplication was not created in the main() thread.
Out[5]: [<matplotlib.lines.Line2D at 0x7fade916a438>]
In[6]: show()
它打开一个窗口并崩溃。我必须停止控制台并启动一个新控制台。
当我在终端的 ipython 控制台中运行类似的东西时,它工作正常,错误似乎只发生在 Pycharm 中。
另一方面,如果导入 matplotlibimport matplotlib.pyplot as plt
它工作正常:
In[2]: import matplotlib.pyplot as plt
In[3]: x = range(5)
In[4]: y = range(5,10)
In[5]: plt.plot(x,y)
Out[5]: [<matplotlib.lines.Line2D at 0x7fd3453b72e8>]
In[6]: plt.show()
但如果我同时执行这两个操作,它也会崩溃(甚至使用plt.plot
):
In[2]: from matplotlib.pyplot import *
In[3]: import matplotlib.pyplot as plt
In[4]: x = range(5)
In[5]: y = range(5,10)
In[6]: plt.plot(x,y)
WARNING: QApplication was not created in the main() thread.
Out[6]: [<matplotlib.lines.Line2D at 0x7fade916a438>]
In[7]: plt.show()
此外,当我在一个命令中运行所有内容时,它第一次就可以工作。但如果我尝试再次绘制,它就会崩溃:
In[2]: from matplotlib.pyplot import *
...: x = range(5)
...: y = range(5,10)
...: plot(x,y)
...: show()
In[3]: plot(x,y)
WARNING: QApplication was not created in the main() thread.
Out[3]: [<matplotlib.lines.Line2D at 0x7fc68a3009e8>]
In[4]: show()
因此,这与使用 matplotlib 库和使用 * 导入以及第一次导入后在交互式控制台中运行有关。我知道不建议使用通配符导入,但有时这样做是有用的,以便更快地测试事物并减少冗长。
在网上寻找这个警告,我只找到了这些
https://github.com/matplotlib/matplotlib/issues/13296 https://github.com/matplotlib/matplotlib/issues/13296但我的情况似乎与多处理无关。即使 pycharm 在幕后做一些事情,我也想知道为什么它发生了变化,因为一个月前我对此没有任何问题;
抑制警告“QApplication 未在 main() 线程中创建” https://stackoverflow.com/questions/27963697/suppress-warning-qapplication-was-not-created-in-main-thread以及其他与 C++ 相关的帖子,这不是我的情况;
警告:QApplication 未在 main() 线程中创建 https://stackoverflow.com/questions/52076262/warning-qapplication-was-not-created-in-main-thread-> 与 pycharm 相关,但有一个与我的不同的额外错误
这并没有多大帮助。任何人都知道发生了什么事以及如何解决它?
SPECS:
- PyCharm 2019.1.2(专业版)
- 内部版本 #PY-191.7141.48,建于 2019 年 5 月 7 日
- JRE:11.0.2+9-b159.56 amd64
- JVM:JetBrains s.r.o 的 OpenJDK 64 位服务器 VM
- Linux 4.15.0-50-通用
- conda 4.6.14,使用 Python 3.7.3
- Qt5