简体   繁体   English

rhel7 matplotlib:“导入错误:没有名为Tkinter的模块”

[英]rhel7 matplotlib: “ImportError: No module named Tkinter”

I am getting the error "ImportError: No module named Tkinter" but don't know why. 我收到错误“ ImportError:没有名为Tkinter的模块”,但不知道为什么。

When I run my script I am getting this error but that is usually do to a library you are trying to use that you have not imported. 当我运行脚本时,出现此错误,但这通常是由于您尝试使用尚未导入的库造成的。 I have searched and all of the posts I have found are directly related to someone ether messing up "Tkinter" with "tkinter" python 2 and 3, or have not imported the library the right way. 我已经搜索过,发现的所有帖子都与有人用“ tkinter” python 2和3弄乱了“ Tkinter”,或者没有以正确的方式导入库有关。 I have ran this script from the command line on ubuntu 14.04, Spyder on the same Ubuntu box, and also on the Cloudera Data Science workbench and it worked fine. 我已经在ubuntu 14.04上的命令行上运行了该脚本,在同一Ubuntu框上的Spyder上运行了该脚本,还在Cloudera Data Science工作台上运行了该脚本,它运行良好。 On all of these machines I have not imported the library and it works flawlessly. 在所有这些机器上,我都没有导入该库,它可以完美运行。

When I try to run it on my rhel 7 box I am getting the following error: 当我尝试在rhel 7框上运行它时,出现以下错误:

root@rhel7_box:/home/user/4688_events_PC-Tags_last_7_days# python 4688_events_PC-Tags_last_7_days_NN_FromMergedHash.py
Traceback (most recent call last):
  File "4688_events_PC-Tags_last_7_days_NN_FromMergedHash.py", line 5, in <module>
import matplotlib.pyplot as plt
  File "/usr/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/lib64/python2.7/site-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
[backend_name], 0)
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 4, in <module>
from . import tkagg  # Paint image to Tk photo blitter extension.
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/tkagg.py", line 5, in <module>
from six.moves import tkinter as Tk
  File "/usr/lib/python2.7/site-packages/six.py", line 203, in load_module
mod = mod._resolve()
  File "/usr/lib/python2.7/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
  File "/usr/lib/python2.7/site-packages/six.py", line 82, in _import_module
__import__(name)
ImportError: No module named Tkinter

It seems to be getting the error when it hits "import matplotlib.pyplot as plt". 当它碰到“ import matplotlib.pyplot as plt”时,似乎正在收到错误。

These are the libraries that I am using that work fine everywhere else: 这些是我在其他地方都能正常使用的库:

#Libraries
import pandas as pd
import numpy as np
import hashlib
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import keras
from keras.models import Sequential
from keras.layers import Dense
from sklearn.metrics import confusion_matrix

tkinter is used as a UI processing backend for matplotlib . tkinter用作matplotlib的UI处理后端。 It should be shipped with python but might not be on your system for various reasons. 它应该随python一起提供,但由于各种原因可能不在您的系统上。 Chances are you you have agg , (should ship with ubuntu) which you can use instead. 您可能有agg ,(应该随ubuntu一起提供),您可以改用它。

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

You can of course still use tkinter if you install it on your box. 如果将tkinter安装在盒子上,则当然仍然可以使用。 If you prefer that for some reason I can let you know but it requires sudo . 如果您出于某种原因喜欢它,我可以通知您,但这需要sudo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM