[英]Problems importing Turtle (Python 3.5)
I'm trying to use Turtle, but for some reason, it won't let me import it. 我正在尝试使用Turtle,但是由于某种原因,它不允许我导入它。 I've tried from turtle import *
(and that works) but if I try print(dir(turtle))
or to use any functions, I get an error saying turtle is not defined. 我已经尝试过from turtle import *
(并且可行),但是如果我尝试使用print(dir(turtle))
或使用任何功能,则会收到一条错误消息,指出未定义turtle。
from turtle import Turtle
doesn't work but print(dir(Turtle))
after using from turtle import *
does work. from turtle import Turtle
不起作用,但从from turtle import *
使用print(dir(Turtle))
可以工作。 However, prefixing commands with Turtle. 但是,在命令前添加Turtle。 ie Turtle.color("red")
doesn't work. 即Turtle.color("red")
不起作用。
Also, the demonstrations in the turtledemo folder work. 此外,turtledemo文件夹中的演示也可以工作。 I'd really appreciate any help 我真的很感谢任何帮助
This imports turtle
into the main namespace and is used as follows: 这会将turtle
导入到主要名称空间中,并按如下方式使用:
import turtle
turtle.something()
Thus, you now have an identifier in your main namespace, named turtle
. 因此,您现在在主命名空间中有一个名为turtle
的标识符。
This imports all visible identifiers from turtle into the main namespace and is used differently: 这会将所有可见的标识符从turtle导入到主要名称空间中,并以不同的方式使用:
from turtle import *
something()
In this scenario, turtle
is not in the main namespace. 在这种情况下, turtle
不在主命名空间中。 Its contents are. 它的内容是。 Thus, dir(turtle)
will fail, because that identifier isn't there. 因此, dir(turtle)
将失败,因为该标识符不存在。
Are you sure that from turtle import Turtle
doesn't work? 您确定from turtle import Turtle
不起作用吗? It worked for me. 它为我工作。
If you import all from turtle
then you have to use the Turtle
object directly, like this: 如果从turtle
导入所有对象,则必须直接使用Turtle
对象,如下所示:
from turtle import *
print(dir(Turtle))
and not like this: 而不是这样:
from turtle import *
print(dir(turtle))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.