关于python 3.x:ImportError:没有名为’_tkinter’的模块,请安装python3-tk包

ImportError: No module named '_tkinter', please install the python3-tk package

在这方面,我已经讨论了所有类似的问题,并尝试了那里提出的解决方案。但我无法解决这个错误,尽管我的python3-tk包安装在我的项目使用的适当的virtualenv中。

虽然在我的项目中,我不使用tkinter,但是当我尝试运行文件时,我得到了与_tkinter模块相关的以下错误。

Traceback (most recent call last):
File"/usr/lib/python3.5/tkinter/init.py", line 36, in
import _tkinter
ImportError: No module named '_tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File"/home/manuelanayantarajeyaraj/PycharmProjects/ChatbotWord2Vec/main.py", line 2, in
from matplotlib import pyplot as plt
File"/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/matplotlib/pyplot.py", line 115, in
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File"/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/matplotlib/backends/init.py", line 62, in pylab_setup
[backend_name], 0)
File"/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 4, in
from . import tkagg # Paint image to Tk photo blitter extension.
File"/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/matplotlib/backends/tkagg.py", line 5, in
from six.moves import tkinter as Tk
File"/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/six.py", line 92, in get
result = self._resolve()
File"/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File"/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/six.py", line 82, in _import_module
import(name)
File"/usr/lib/python3.5/tkinter/init.py", line 38, in
raise ImportError(str(msg) + ', please install the python3-tk package')
ImportError: No module named '_tkinter', please install the python3-tk package

因此,我导航到解释器的位置,创建了一个virtualenv,并使用以下方法安装了python3-tk

1
sudo apt-get install python3-tk

当我检查时,所有的包裹似乎都是最新的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Reading package lists... Done
Building dependency tree      
Reading state information... Done
python3-tk is already the newest version (3.6.5-3~16.04.york0.2).
The following packages were automatically installed and are no longer required:
  libappindicator1 libindicator7 libllvm4.0 linux-headers-4.10.0-28
  linux-headers-4.10.0-28-generic linux-headers-4.13.0-36
  linux-headers-4.13.0-36-generic linux-headers-4.13.0-37
  linux-headers-4.13.0-37-generic linux-image-4.10.0-28-generic
  linux-image-4.13.0-36-generic linux-image-4.13.0-37-generic
  linux-image-extra-4.10.0-28-generic linux-image-extra-4.13.0-36-generic
  linux-image-extra-4.13.0-37-generic linux-signed-image-4.10.0-28-generic
  linux-signed-image-4.13.0-36-generic linux-signed-image-4.13.0-37-generic
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 37 not upgraded.

但我仍然得到同样的导入错误ImportError: No module named '_tkinter', please install the python3-tk package

在这方面的任何建议都将受到高度赞赏。


当您导入matplotlib时,它可能会尝试使用tk后端作为默认后端。如果您没有安装tk,或者您不想在项目中的任何其他地方使用它,那么一个可能的解决方案就是简单地使用一个不同的后端:

1
2
3
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt


消息表明,当运行sudo apt-get install python3-tk时,它告诉您tkinter是为python3.6.5而初始化的,但另一方面,ImportError与python3.5相关。所以我认为这应该解决你的问题:

1
sudo apt-get install python3.5-tk