4

When I try to import the python 3 graphical library, turtle, on my m1 MacBook, I get an error message:

 david@Davids-MacBook-Air Python Coding Files % /opt/homebrew/bin/python3 "/Users/david/Desktop/Python Coding Files/hello.py"
Traceback (most recent call last):
  File "/Users/david/Desktop/Python Coding Files/hello.py", line 1, in <module>
    import turtle
  File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/turtle.py", line 107, in <module>
    import tkinter as TK
  File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 37, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

I see it has the library tkinter written in it for some reason. This is weird because on my other Mac, an intel based Mac, with the same python 3 version, and no library such as tkinter install, I didnt get an error message when trying to import turtle

Does anybody know what the problem is?

1 Answers1

6

Python 3's turtle module has always used tkinter. Your other Mac must have tkinter installed. It used to be shipped with Python, but now you need to install it. Assuming you use HomeBrew:

brew install python-tk
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Did you do any web searching on your own before asking this question? `pip install tk` – Tim Roberts Aug 21 '21 at 06:14
  • not before, but after. I did that cuz I didn't really know if `brew install python-tk` will install it for python 3.9. I've noticed some others doing `brew install python-tk@3.9`, do you think I should do that, the more specified command? –  Aug 21 '21 at 06:20
  • You ought to be able to figure this out. What version of Python has your homebrew installed? You can see that in your traceback. If you use homebrew, it installs the packages that fit the homebrew-installed Python. And why didn't you just try it? What is the cost if you're wrong? – Tim Roberts Aug 21 '21 at 06:26
  • my homebrew installed python 3.9.6, installed it using the `brew install python@3.9` command –  Aug 21 '21 at 06:35
  • I also have python 3.8.2 that got installed with the command line tools, and so that's why im hesitate and think that it might accidentally install tk for python 3.8.2 instead of python 3.9.6 –  Aug 21 '21 at 17:13
  • Homebrew will install addons to what homebrew installed. Even if it did install it to the system Python, what's the harm? It's quicker to try it than it is to wait for answers here. – Tim Roberts Aug 22 '21 at 00:03
  • 1
    because its not recommended to install things for the system python –  Aug 22 '21 at 05:29
  • Maybe, but you're not going to hurt anything. That's why homebrew manages its own installation. – Tim Roberts Aug 22 '21 at 06:45
  • ok, I will use the `brew install python-tk` command to install tkinter for python 3.9.6 –  Aug 22 '21 at 06:49
  • It seems as if it does not work with "port" (?) – Coliban Jun 03 '22 at 10:48