7

I am an absolute newbie. I'm trying to make Python GUI for my school project so I decided to use Tkinter. When I try to import Tkinter it throws this message:

>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

I tried to find a solution online but I couldn't figure it out (mostly didn't understand it).

I read about some problem with directory in setup.py but I don't understand how to fix it. I have tkinter folder in my python3.7 folder.

I don't really understand these steps that I found:

If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.

I'm using Mac OS and use Visual Studio Code.

Yaniv Oliver
  • 3,372
  • 1
  • 19
  • 20
PanFousek
  • 71
  • 1
  • 1
  • 2

2 Answers2

5

To check your python version, run this on terminal:

$ python --version

Check what python your machine is using. Run:

$ which python

If it's using python from Homebrew, it's probably using Python 2. If you installed python 3 manually, just install tKinter manually.

$ brew install python-tk

To run python 2 manually, run on terminal:

$ python <FILENAME>

If python 3, run:

$ python3 <FILENAME>
schoolboy
  • 71
  • 1
  • 7
1

https://stackoverflow.com/a/9883299/4678635 will help you.

In summary, you have to reinstall python for your computer bit system.

And below code strategy is also helpful to you.

try:    
    from Tkinter import * # for Python2
except ImportError:
    from tkinter import * # for Python3
Minu
  • 380
  • 2
  • 12
  • Thanks for the advice! It works now. But it shows me this notification "DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning." Should I silence it or is there a way to update it? – PanFousek Apr 20 '20 at 14:29
  • I am not sure it works you. https://stackoverflow.com/a/58757261/4678635. But It it wortht to try. – Minu Apr 20 '20 at 22:08