ModuleNotFoundError: No module named '_tkinter' on Jupyter Notebook

Viewed 444

i'm trying to put some code in jupyter notebook, and for that, i need to import tkinter.

I'm doing this

from tkinter import *

And i have this error :

---------------------------------------------------------------------------
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'

But when i'm doing in local python3 script.py everything is working. And i have tkinter in my pc. I'm using Fedora. I've already tried to reinstall but nothing is working. Anyone have a clue ?

1 Answers

The anwser of furas was correct. My problem was I had two python3 and jupyter notebook didn't have the good version.

Basically, to fix this problem :

 $which python

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

Now i know the path of the python with my tkinter. I just need to find the kernel of my jupyter notebook and update the kernel.json with the path of my actual python

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

When i'm opening my kernel.json i have

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

I just need to add the path of my python3

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

And now, i can use tkinter (or another) in jupyter notebook

Related