Python Forum

Full Version: [SOLVED] Tkinter module not found
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello team,

So I installed tkinter using

pip3 install tk
But when I try to run the following code

import tkinter as tk

root = tk.Tk()
root.mainloop()
I get the following error:
Traceback (most recent call last):
  File "iwannadie.py", line 1, in <module>
    import tkinter as tk
ModuleNotFoundError: No module named 'tkinter'
This is driving me crazy, because I have the tk lib installed

pip3 list | grep tk             
tk                             0.1.0    


And tried to import it in different ways

import tkinter as tk
from tkinter import *
import tk


Nothing seems to work, could you please help?

[SOLVED]

Run command
sudo apt-get install python3-tk
Tkinter comes with Python. tk that you've installed is something else: https://pypi.org/project/tk/.

It's odd that you can't import tkinter, though. More information, please. Which Python version? Are you using a virtual environment, or have you got any custom set up in terms of where Python is looking for libraries?
Currently using python 3.8, not a virtual environment.
(Aug-02-2022, 07:17 PM)ndc85430 Wrote: [ -> ]Tkinter comes with Python.

Not always: I had to sudo apt-get install python3-tk on my Linux system.

Quote:Ubuntu Linux 20.04:

To conserve memory space, the default version of the Python interpreter that comes pre-installed on Ubuntu Linux 20.04 has no support for Tkinter. However, if you want to continue using the Python interpreter bundled with your operating system, then install the following package:

link: https://realpython.com/python-gui-tkinter/
I usually do python3 -m pip install module. Ensures that I'm installing to the correct python version.
(Aug-02-2022, 07:21 PM)Milan Wrote: [ -> ]Currently using python 3.8, not a virtual environment.

Running sudo apt-get install python3-tk solved it, I am also on linux. Thank you very much.
FYI, if your software manager does not resolve dependencies like Ubuntu does. you may also have to install TCL and TK. Tkinter is just a Python wrapper around the tool kit.