pycharm AttributeError中无法执行代码'matplotlib.pyplot as plt':'InputHookManager'对象没有属性'_stdin_file'

时间:2019-07-29 20:06:42

标签: python matplotlib pycharm

嗨,我正在使用Ubuntu:和Pycharm最新版本(2019.2): 如果我仅导入matplotlib,则不会显示任何错误, 但是,如果我尝试执行matplotlib.pyplot作为plt它显示我 出现以下错误:请参阅下面的注释部分

import matplotlib.pyplot as plt
Backend GTK3Agg is interactive backend. Turning interactive mode 
on.
Traceback (most recent call last):
File "/home/sarfi/.local/lib/python3.7/site- 
packages/IPython/core/interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-a0d2faabd9e9>", line 1, in <module>
import matplotlib.pyplot as plt
File "/snap/pycharm- 
community/143/helpers/pydev/_pydev_bundle/pydev_import_hook.py", 
line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/sarfi/.local/lib/python3.7/site- 
packages/matplotlib/pyplot.py", line 2362, in <module>
install_repl_displayhook()
File "/home/sarfi/.local/lib/python3.7/site- 
packages/matplotlib/pyplot.py", line 126, in 
install_repl_displayhook
ip.enable_gui(ipython_gui_name)
File "/snap/pycharm-community/143/helpers/pydev/_pydev_bundle/pydev_ipython_console_011.py", line 146, in enable_gui
return real_enable_gui(gui, app)
File "/snap/pycharm-community/143/helpers/pydev/pydev_ipython/inputhook.py", line 536, in enable_gui
return gui_hook(app)
File "/snap/pycharm-community/143/helpers/pydev/pydev_ipython/inputhook.py", line 413, in enable_gtk3
self.set_inputhook(create_inputhook_gtk3(self._stdin_file))
AttributeError: 'InputHookManager' object has no attribute '_stdin_file'

注意:我尝试卸载matplotlibpip3并重新安装, 我还安装了Sixpip3 install Six,但是问题仍然存在 一样

1 个答案:

答案 0 :(得分:0)

这里有一个解决方法: 使用不同的后端(来自 gtk/gtk3)

import matplotlib                #only import mpl first
matplotlib.use("agg")            #then set backend
import matplotlib.pyplot as plt 

plt.figure()
plt.plot([1,1])
plt.savefig("name.jpg")

我还没有测试其他后端,但这有效。当然,这不是永久的解决方案,而是如果您需要数字的快速紧急解决方案。

问题在于 gtk/gtk3(我认为)。 还显示在 PY-16043 无法为“gtk”启用 GUI 事件循环集成 (参见 https://youtrack.jetbrains.com/issue/PY-16043 ,这是 BugsBunny 已经放置的链接)。

定义 enable_gtk/enable_gtk3 方法时发生错误。所以如果你不使用这个后端,你就可以避免这个错误。