首页 文章

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

提问于
浏览
5

虽然我的 python3-tk 软件包安装在我用于我的项目的正确的virtualenv中,但是've already gone through all the similar questions in this regard and tried the solutions proposed there. But I' m无法解决此错误 .

虽然在我的项目中,我没有't use tkinter, when i try to run the file, I' m得到与 _tkinter 模块相关的以下错误 .

Traceback(最近调用最后一次):文件“/usr/lib/python3.5/tkinter/init.py”,第36行,在import _tkinter中ImportError:没有名为'_tkinter'的模块在处理上述异常时,另一个例外发生:回溯(最近一次调用最后一次):文件“/home/manuelanayantjejeyaraj/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/init.py“,第38行,在引发ImportError(str(msg)',请安装python3-tk包' )ImportError:没有名为'_tkinter'的模块,请安装python3-tk包

因此,我导航到我的解释器的位置并创建了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 .

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

2 回答

  • 5

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

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

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

    sudo apt-get install python3.5-tk
    

相关问题