python 中的 Turtle 模块未导入
Turtle Module in python not importing
这是我第一次在 python 中使用 turtle 模块,但我似乎无法导入它?
这是我的代码:
from turtle import *
pen1 = Pen()
pen2 = Pen()
pen1.screen.bgcolour("#2928A7")
这是我得到的错误:
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
谁能告诉我我做错了什么?
问题是您将程序命名为 "turtle.py"。
所以当Python看到语句
from turtle import *
它找到的第一个名为 turtle
的匹配模块是 你的 程序 "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
Written by PM 2Ring 2015.08.24
'''
import turtle
foo = 42
print(turtle.foo)
help(turtle)
我想我应该展示代码实际打印的内容...
当 运行 为 turtle.py
时,它打印以下 "help" 信息:
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
Written by PM 2Ring 2015.08.24
DATA
foo = 42
(END)
当您点击 Q
退出帮助时,帮助信息会再次显示。当您第二次点击 Q
时,则
42
42
已打印。
为什么 "help" 消息和 42 打印了两次?这是因为 turtle.py
中的所有代码在导入时执行,然后在遇到 after import
语句时再次执行。请注意 Python 不会尝试导入它已经导入的模块(除非使用 reload
明确告知这样做)。如果Pythondid重新导入,那么上面的代码就会陷入导入的死循环。
当 运行 作为 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'
当然那是因为标准 turtle
模块实际上没有 foo
属性。
我认为解决方案是输入:
pen1 = turtle.Pen()
这是我第一次在 python 中使用 turtle 模块,但我似乎无法导入它?
这是我的代码:
from turtle import *
pen1 = Pen()
pen2 = Pen()
pen1.screen.bgcolour("#2928A7")
这是我得到的错误:
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
谁能告诉我我做错了什么?
问题是您将程序命名为 "turtle.py"。
所以当Python看到语句
from turtle import *
它找到的第一个名为 turtle
的匹配模块是 你的 程序 "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
Written by PM 2Ring 2015.08.24
'''
import turtle
foo = 42
print(turtle.foo)
help(turtle)
我想我应该展示代码实际打印的内容...
当 运行 为 turtle.py
时,它打印以下 "help" 信息:
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
Written by PM 2Ring 2015.08.24
DATA
foo = 42
(END)
当您点击 Q
退出帮助时,帮助信息会再次显示。当您第二次点击 Q
时,则
42
42
已打印。
为什么 "help" 消息和 42 打印了两次?这是因为 turtle.py
中的所有代码在导入时执行,然后在遇到 after import
语句时再次执行。请注意 Python 不会尝试导入它已经导入的模块(除非使用 reload
明确告知这样做)。如果Pythondid重新导入,那么上面的代码就会陷入导入的死循环。
当 运行 作为 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'
当然那是因为标准 turtle
模块实际上没有 foo
属性。
我认为解决方案是输入:
pen1 = turtle.Pen()