Tango
Tango

Reputation: 669

Cannot use tkinter in Python 3 on macOS

Short version: Python 3 is unable to use tkinter on my iMac.

Details and useful info: I'm on macOS Mojave, Version 10.14.3 (18D109). (I can't upgrade for a while yet because of software that won't run on the newer versions.)

In my scripts I'm using #!/usr/bin/env python3 and when I get to the line import tk as tkinter I get this error:

Traceback (most recent call last):
  File "/Users/hal/Documents/Dev/HalPy/TestScript.py", line 5, in <module>
    import tkinter
  File "/Users/hal/.pyenv/versions/3.7.3/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'

When I tested it, from the command line, I typed python and got the Python shell and the version info (Python 3.7.3 (default, Apr 11 2020, 02:53:05)). Then I tried import tkinter as tk and I get this:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/hal/.pyenv/versions/3.7.3/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 see the module when I drop into the Help mode and type modules and tkinter shows up in the list of modules. Just in case, I tried installing it with pip, but it wouldn't install (which is what I expected).

So what do I need to do to get Python 3 to recognize tkinter? (Just in case it matters, Python 2 with Tkinter does work.)

Also, I have looked at this question and this one and this is NOT a duplicate. (First note that neither has an accepted answer.) The first one involves reinstalling, and the focus seems to be on Windows, and this is on macOS, which includes it pre-installed. I'm a bit concerned what reinstalling it would do. The 2nd question is having problems because it's being run within the XCode framework and one element of the issue is the wrong shebang and I'm already using the one the answer suggests.

Upvotes: 2

Views: 5765

Answers (1)

A.Peerally
A.Peerally

Reputation: 1

If you are using a Python from any current python.org Python installer for macOS (3.9.0+, 3.8.0+, or 3.7.2+), no further action is needed to use IDLE or tkinter. A built-in version of Tcl/Tk 8.6 will be used.

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.

Upvotes: 0

Related Questions