【问题标题】:ModuleNotFoundError: No module named '_tkinter' on Jupyter NotebookModuleNotFoundError:Jupyter Notebook 上没有名为“_tkinter”的模块
【发布时间】:2021-11-08 17:43:30
【问题描述】:

我正在尝试将一些代码放入 jupyter notebook,为此,我需要导入 tkinter。

我正在这样做

from tkinter import *

我有这个错误:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-6-67079ccbcb8c> in <module>
      1 import nltk
----> 2 from tkinter import *
      3 from nltk.chat.util import Chat, reflections
      4 from nltk.corpus import wordnet as wn
      5 import string

/usr/local/lib/python3.7/tkinter/__init__.py in <module>
     34 import sys
     35 
---> 36 import _tkinter # If this fails your Python may not be configured for Tk
     37 TclError = _tkinter.TclError
     38 from tkinter.constants import *

ModuleNotFoundError: No module named '_tkinter'

但是,当我在本地 python3 script.py 工作时,一切正常。 我的电脑里有 tkinter。我正在使用 Fedora。 我已经尝试重新安装,但没有任何效果。有人知道吗?

【问题讨论】:

  • 你读过this question吗?
  • @SylvesterKruin 是的,但它给了我:错误:找不到满足 python-tk 要求的版本(来自版本:无)错误:没有找到 python-tk 的匹配分布
  • 等等——我搞混了。您需要使用 system 命令安装tkinter,而不是python3。很抱歉造成混乱:D!尝试在 Stack Overflow 上搜索 [python] install tkinter fedora。通过简单搜索判断,可以使用sudo yum install tkinteryum install tkinter
  • 嗯...对不起,我恐怕在这里帮不上什么忙了。我不使用 Fedora 或 Jupyter 笔记本,所以我对它们不够熟悉,无法知道这里出了什么问题:-(。
  • 也许你安装了两个 Python,你用一个 Python 运行 jupyter,但 python script.py 用另一个 Python 运行它。在 Jupyter 中,您可以使用 sys.executable() 获取 /full/path/to/python 并将其与控制台中的 which python 进行比较。

标签: python tkinter jupyter-notebook


【解决方案1】:

furas 的回答是正确的。我的问题是我有两个 python3 和 jupyter notebook 没有好的版本。

基本上,解决这个问题:

 $which python

-> alias python='/usr/bin/python3.7'
    /usr/bin/python3.7

现在我用我的 tkinter 知道了 python 的路径。 我只需要找到我的 jupyter notebook 的内核并用我的实际 python 的路径更新 kernel.json

$jupyter kernelspec list
-> Available kernels:
    python3    /home/Natko/.local/share/jupyter/kernels/python3
$nano  /home/Natko/.local/share/jupyter/kernels/python3/kernel.json 

当我打开我的 kernel.json 时,我有

{
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python",
}

我只需要添加我的python3的路径

{
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python",
 "env": {
     "PYTHONPATH": "/usr/bin/"
 }
}

现在,我可以在 jupyter notebook 中使用 tkinter(或其他)

【讨论】:

    猜你喜欢
    • 2021-06-20
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2021-10-03
    • 2017-10-29
    • 2020-11-08
    • 2020-10-25
    • 1970-01-01
    相关资源
    最近更新 更多