python中的海龟模块没有导入 [英] Turtle Module in python not importing

查看:41
本文介绍了python中的海龟模块没有导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在 python 中使用乌龟模块,但我似乎无法导入它?
这是我的代码:

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

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

谁能告诉我我做错了什么?

Can anyone tell me what I did wrong?

推荐答案

问题是您将程序命名为turtle.py".

The problem is that you've named your program "turtle.py".

所以当 Python 看到语句时
从海龟进口*
它找到的第一个名为 turtle 的匹配模块是 您的 程序turtle.py".

So when Python sees the statement
from turtle import *
the first matching module named turtle that it finds is your program, "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

#! /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...

当以 turtle.py 运行时,它会打印以下帮助"信息:

When run as turtle.py it prints the following "help" info:

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) 

当您点击 Q 退出帮助时,帮助信息将再次显示.当你第二次点击Q,然后

When you hit Q to get out of the Help, the Help info is displayed again. When you hit Q for the second time, then

42

42

被打印出来.

为什么帮助"信息和 42 打印了两次?这是因为turtle.py 中的所有代码在导入时都会执行,然后在遇到 import 语句后再次执行.请注意,Python 不会尝试导入它已经导入的模块(除非使用 reload 明确告诉这样做).如果 Python 确实重新导入,那么上面的代码将陷入无限的导入循环.

Why are the "help" message and 42 printed twice? 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. Note that Python doesn't try to import modules that it has already imported (unless explicitly told to do so with reload). If Python did re-import, then the above code would get stuck in an infinite loop of importing.

当以 mockturtle.py 运行时,它会打印:

When run as mockturtle.py it prints:

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 属性.

And of course that's because the standard turtle module doesn't actually have a foo attribute.

这篇关于python中的海龟模块没有导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆