尝试在RHEL Fedora系统上导入matplotlib时,出现此错误:
Traceback (most recent call last):
File "generate_example_images.py", line 9, in <module>
import matplotlib.pyplot as plt
File "/usr/local/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/local/lib64/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/usr/local/lib64/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from six.moves import tkinter as Tk
File "/usr/lib/python2.7/dist-packages/six.py", line 194, in load_module
mod = mod._resolve()
File "/usr/lib/python2.7/dist-packages/six.py", line 108, in _resolve
return _import_module(self.mod)
File "/usr/lib/python2.7/dist-packages/six.py", line 79, in _import_module
__import__(name)
ImportError: No module named Tkinter
https://tkinter.unpythonic.net/wiki/How_to_install_Tkinter的另一个问题是我可以使用以下内容:
yum install tkinter
这应该为python 2和python3安装一个tkinter包,但是,然后我得到错误
Loaded plugins: priorities, update-motd, upgrade-helper
amzn-main/latest | 2.1 kB 00:00
amzn-updates/latest | 2.3 kB 00:00
No package tkinter available.
Error: Nothing to do
当我看到yum包时,我也没有看到tkinter包,所以我有理由得到这个错误。
答案 0 :(得分:1)
在Python 2.7中,Tkinter包名称为Tkinter
,而不是Python 3 tkinter
。当我查看您的错误时,您的matplotlib
似乎是Python 3脚本。
如果您自己编写matplotlib
(/usr/local/lib64/python2.7/site-packages/matplotlib/backends/backend_tkagg.py
)(并且是为Python 2.7编写的),请从
from six.moves import tkinter as Tk
到
from six.moves import Tkinter as Tk
或更加版本友好:
try:
from six.moves import tkinter as Tk
# print('Python 3')
except ImportError e:
from six.moves import Tkinter as Tk
# print 'Python 2.7'
如果您还没有自己编写matplotlib
,请为Python 2.7找到不同的安装(呃)。如果此操作不起作用,请使用上述修补程序并通过与matplotlib
的管理员联系来报告问题。