8

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.

3 Answers 3

6

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

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?
I am not sure it works you. stackoverflow.com/a/58757261/4678635. But It it wortht to try.
0

The simplest solution to this is to install Python from the Python.org website, as outlined here: https://www.python.org/download/mac/tcltk/

If you are using macOS 10.6 or later, the Apple-supplied Tcl/Tk 8.5 has serious bugs that can cause application crashes. If you wish to use IDLE or Tkinter, do not use the Apple-supplied Pythons. Instead, install and use a newer version of Python from python.org or a third-party distributor that supplies or links with a newer version of Tcl/Tk.

I went around and around trying to get this to work but once I installed the binary directly from the website instead of what is provided via brew, it all started working without issue.

1 Comment

Seems okay now with Python 3.13 installed by Homebrew and running brew install python-tk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.