2

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.

Tango
  • 649
  • 1
  • 12
  • 29
  • See [here](https://www.python.org/download/mac/tcltk/) – Paul Rooney Apr 25 '21 at 07:23
  • try to run `brew install tcl-tk brew link tcl-tk --force` – Rocket Nikita Apr 25 '21 at 07:24
  • @RocketNikita: I did that and got an error message telling me to use echo to add a line to my .bash_profile. So I opened .bash_profile and added the specified path to where I set $PATH and opened a new shell that would include that path in $PATH. It still gives me the same error - so the path is there and it's complaining that it isn't. – Tango Apr 25 '21 at 07:35
  • @PaulRooney Is it me, or does a new installer only put a new version of Python in /Applications and does nothing to update any version of Python in /bin or /usr/bin or similar locations? That's what I'm finding. It includes Python Launcher, so I'd have to change the shebang in each script to point to /Applications/Python/Contents/MacOS/Python Launcher. – Tango Apr 25 '21 at 08:19
  • I'm sorry I have no idea. I'm a windows/linux user. Just hoped the link would help. One of the answer below is quoting it. – Paul Rooney Apr 25 '21 at 08:59
  • Not adding this as an answer, but for those looking for more info, I did get the problem solved. I upgraded from Mojave to Catalina, then added the Security Update for Catalina. This is an older iMac, so I can't upgrade to Big Sur. I had a number of configuration issues to deal with along the way and didn't get to check on this until I had done both updates and taken care of other things. Something in the updates fixed it and I'm not having this problem anymore. – Tango May 03 '21 at 05:56

2 Answers2

1

you can find your answer here: https://blog.lanzani.nl/2020/install-tkinter-macos/

0

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.

A.Peerally
  • 19
  • 2