问题
Every time I tried to run import matplotlib
I have error ImportError: No module named Tkinter
the output result as listed below
Python 2.7.5 (default, Aug 2 2016, 04:20:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/usr/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/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
I tried to install tk.x86_64
tk-devel.x86_64
packages and tried to reinstall python package but the error still appeared.
Also I tried to import Tkinter
but i got the following error
Python 2.7.5 (default, Aug 2 2016, 04:20:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Tkinter
Does anyone have workaround for this error?
回答1:
I tried to install tkinter package for python2.7.5 from the following link:
tkinter package
Also I found there is dependency library libTix.so()(64bit) for tkinter package and i got it from the following link: libTix.so()(64bit) package
after that i installed both then I could import Tkinter
and import matplotlib.pyplot as plt
with no errors.
回答2:
I'm using python2.7.12 on ubuntu16.04. I installed it with this command:
sudo apt-get install python-tk
回答3:
My 2 cents. Open a terminal and type the following.
$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>>
If you do not get an error like below then your Tkinter is fine and the problem is with matplotlib. Then try removing and reinstalling matplotlib.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named tkinter
回答4:
I was getting the error "ImportError: No module named '_tkinter'" for python3.5 in-spite of installing python3-tk and tkinter-dev.
It showed the error in following file:
File "/usr/local/lib/python3.5/tkinter/init.py", line 35, in import _tkinter # If this fails your Python may not be configured for Tk
It got resolved for me by simply running the python from /usr/bin like /usr/bin/python3.5 from terminal!
In short, please ensure the python version being run from terminal has the tkinter installed and configured :)
My PC configuration: Ubuntu16.04 with default python2.7 and python3.5
来源:https://stackoverflow.com/questions/42435315/python-importerror-no-module-named-tkinter