尝试导入 turtle 时收到错误消息(python 3.9,m1 Mac)

Error message received when trying to import turtle (python 3.9, m1 Mac)

当我尝试在我的 m1 MacBook 上导入 python 3 图形库 turtle 时,我收到一条错误消息:

 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'

我看到它出于某种原因在其中写入了库 tkinter。这很奇怪,因为在我的另一个 Mac 上,基于英特尔的 Mac,具有相同的 python 3 版本,并且没有安装 tkinter 等库,我没有收到错误消息尝试导入时 turtle

有人知道问题出在哪里吗?

Python 3 的turtle 模块一直使用tkinter。您的其他 Mac 必须安装 tkinter。它曾经随 Python 一起提供,但现在您需要安装它。假设您使用 HomeBrew:

brew install python-tk