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

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
sudo apt-get install python3-tk

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

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,或者您不想在项目的其他任何地方使用它,那么一个可能的解决方案是简单地使用不同的后端:

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

该消息表明,当您 运行 sudo apt-get install python3-tk 它告诉您 tkinter 已安装 Python3.6.5,但在另一方面,ImportErrorPython3.5 有关。所以我相信这应该可以解决您的问题:

sudo apt-get install python3.5-tk