ImportError:没有名为'_tkinter'的模块,请安装python3-tk包

Nay*_*raj 11 tkinter matplotlib importerror python-3.x ubuntu-16.04

我已经完成了这方面的所有类似问题,并尝试了那里提出的解决方案.但我无法解决这个错误,虽然我的python3-tk软件包安装在我正在用于我的项目的正确的virtualenv中.

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

回溯(最近通话最后一个):
文件"/usr/lib/python3.5/tkinter/ 初始化 py"为36行,在进口_tkinter
导入错误:没有模块名为"_tkinter"

在处理上述异常期间,发生了另一个异常:

回溯(最近一次调用最后一次):
文件"/home/manuelanayantjeyayaj/PycharmProjects/ChatbotWord2Vec/main.py",第2行,来自matplotlib import pyplot作为plt
文件"/ home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3 .5/site-packages/matplotlib/pyplot.py",第115行,在_backend_mod中,new_figure_manager,draw_if_interactive,_show = pylab_setup()
文件"/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site- packages/matplotlib/backends/init .py",第62行,在pylab_setup [backend_name],0)
文件"/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg .py",第4行,来自.import tkagg#将图像绘制到Tk照片阻挡扩展.
文件"/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/matplotlib/backends/tkagg.py",第5行,来自six.moves import tkinter as Tk
File"/ home/manuelanayantarajeyaraj /usr/myProject/my_project/lib/python3.5/site-packages/six.py",第92行,在get result = self._resolve()
文件"/ home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3 .5/site-packages/six.py",第115行,在_resolve中返回_import_module(self.mod)
文件"/home/manuelanayantarajeyaraj/usr/myProject/my_project/lib/python3.5/site-packages/six.py "线82,在_import_module 进口(名称)
文件"/usr/lib/python3.5/tkinter/ 初始化 py"为38行,在提高导入错误(STR(MSG)+",请安装python3-TK包')
ImportError:没有名为'_tkinter'的模块,请安装python3-tk包

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

sudo apt-get install python3-tk
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)

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

任何有关这方面的建议都将受到高度赞赏.

Dav*_*idG 23

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

import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)


Bil*_*adj 7

该消息表明,当你运行sudo apt-get install python3-tk它时告诉你tkinter是为Python3.6.5设置的,但另一方面,ImportError它与Python3.5有关.所以我相信这应该可以解决你的问题:

sudo apt-get install python3.5-tk
Run Code Online (Sandbox Code Playgroud)

  • 我有相反的问题,但知道可以指定子版本为我解决了问题.在Ubuntu 16上,默认的python 3版本是3.5.在Ubuntu 16上,你需要花一点力气才能让3.6运行,我做了.但是,默认的tk版本保持3.5.所以指定python3.6-tk让我的系统安装正确的tk. (3认同)