Python module turtle not importing correctly

六眼飞鱼酱① 提交于 2021-01-29 11:33:32

问题


This is my fist time using the turtle module in python but i can't seem to import it? Here's my code:

import turtle

turtle.shape("turtle")
turtle.speed(1)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)

turtle.exitonclick()

I run this as $ python3 example.py And I get

$ python3 example.py
Traceback (most recent call last):
File "example.py", line 1, in <module>
from turtle import *
File "/usr/lib/python3.7/turtle.py", line 107, in <module>
import tkinter as TK
ModuleNotFoundError: No module named 'tkinter'

If I run Python2 python example.py

$ python example.py
Traceback (most recent call last):
File "example.py", line 1, in <module>
import turtle
File "/usr/lib/python2.7/lib-tk/turtle.py", line 107, in <module>
import Tkinter as TK
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in <module>
raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk package

回答1:


It was

sudo apt install python3-tkinter



回答2:


This is a simple one to solve:

sudo apt-get install python3-tk

One of the other commenters suggested python-tk which wouldn't work for you here.



来源:https://stackoverflow.com/questions/61502272/python-module-turtle-not-importing-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!