简体   繁体   中英

rhel7 matplotlib: “ImportError: No module named Tkinter”

I am getting the error "ImportError: No module named Tkinter" but don't know why.

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. 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. 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:

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".

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 . It should be shipped with python but might not be on your system for various reasons. Chances are you you have agg , (should ship with ubuntu) which you can use instead.

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

You can of course still use tkinter if you install it on your box. If you prefer that for some reason I can let you know but it requires sudo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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