简体   繁体   English

python中的Turtle模块没有导入

[英]Turtle Module in python not importing

this is my fist time using the turtle module in python but i can't seem to import it? 这是我第一次在python中使用turtle模块,但我似乎无法导入它?
Here's my code: 这是我的代码:

from turtle import *

pen1 = Pen()
pen2 = Pen()

pen1.screen.bgcolour("#2928A7") 

and here is the error I get: 这是我得到的错误:

Traceback (most recent call last):
  File "C:\Python34\Python saves\turtle.py", line 2, in <module>
    from turtle import *
  File "C:\Python34\Python saves\turtle.py", line 5, in <module>
    pen1 = Pen()
NameError: name 'Pen' is not defined

Can anyone tell me what I did wrong? 谁能告诉我我做错了什么?

The problem is that you've named your program "turtle.py". 问题是你已经将你的程序命名为“turtle.py”。

So when Python sees the statement 所以当Python看到这个陈述时
from turtle import *
the first matching module named turtle that it finds is your program, "turtle.py". 它找到的第一个名为turtle匹配模块是你的程序“turtle.py”。

In other words, your program is basically importing itself and not the turtle graphics module. 换句话说,您的程序基本上是导入自己而不是乌龟图形模块。


Here's some code to demonstrate this problem. 这里有一些代码来演示这个问题。

turtle.py turtle.py

#! /usr/bin/env python

''' Mock Turtle

    Demonstrate what happens when you give your program the same name
    as a module you want to import.

    See http://stackoverflow.com/q/32180949/4014959

    Written by PM 2Ring 2015.08.24
'''

import turtle

foo = 42
print(turtle.foo)
help(turtle)

I guess I should show what that code actually prints... 我想我应该展示代码实际打印的内容......

When run as turtle.py it prints the following "help" info: 当以turtle.py运行时,它会打印以下“帮助”信息:

Help on module turtle:

NAME
    turtle - Mock Turtle

FILE
    /mnt/sda4/PM2Ring/Documents/python/turtle.py

DESCRIPTION
    Demonstrate what happens when you give your program the same name
    as a module you want to import.

    See http://stackoverflow.com/q/32180949/4014959

    Written by PM 2Ring 2015.08.24

DATA
    foo = 42

(END) 

When you hit Q to get out of the Help, the Help info is displayed again. 当您点击Q退出帮助时,会再次显示帮助信息。 When you hit Q for the second time, then 当你第二次点击Q时,那么

42

42

is printed. 打印出来。

Why are the "help" message and 42 printed twice? 为什么“帮助”消息和42打印两次? That's because all the code in turtle.py is executed when it's imported, and then again when its encountered after the import statement. 这是因为turtle.py所有代码都是在导入时执行的,然后在import语句遇到它时再次执行。 Note that Python doesn't try to import modules that it has already imported (unless explicitly told to do so with reload ). 请注意,Python不会尝试导入已导入的模块(除非明确告知reload )。 If Python did re-import, then the above code would get stuck in an infinite loop of importing. 如果没有的Python重新导入,那么上面的代码会陷入进口的无限循环。


When run as mockturtle.py it prints: 当以mockturtle.py运行时,它会打印:

Traceback (most recent call last):
  File "./mock_turtle.py", line 16, in <module>
    print(turtle.foo)
AttributeError: 'module' object has no attribute 'foo'

And of course that's because the standard turtle module doesn't actually have a foo attribute. 当然那是因为标准的turtle模块实际上没有foo属性。

我认为解决方案是输入:

pen1 = turtle.Pen()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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