简体   繁体   中英

Problems importing Turtle (Python 3.5)

I'm trying to use Turtle, but for some reason, it won't let me import it. 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 Turtle doesn't work but print(dir(Turtle)) after using from turtle import * does work. However, prefixing commands with Turtle. ie Turtle.color("red") doesn't work.

Also, the demonstrations in the turtledemo folder work. I'd really appreciate any help

This imports turtle into the main namespace and is used as follows:

import turtle
turtle.something()

Thus, you now have an identifier in your main namespace, named turtle .

This imports all visible identifiers from turtle into the main namespace and is used differently:

from turtle import *
something()

In this scenario, turtle is not in the main namespace. Its contents are. Thus, dir(turtle) will fail, because that identifier isn't there.

Are you sure that from turtle import Turtle doesn't work? It worked for me.

If you import all from turtle then you have to use the Turtle object directly, like this:

from turtle import *

print(dir(Turtle))

and not like this:

from turtle import *

print(dir(turtle))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM