【问题标题】:python turtle on mac not workingmac上的python乌龟不工作
【发布时间】:2013-02-20 18:18:46
【问题描述】:

我正在尝试使用海龟的 python 导入,但出现错误:

Traceback (most recent call last):
  File "turtle.py", line 1, in <module>
    import turtle
  File "/turtle.py", line 32, in <module>
    turtle.pensize(2)
AttributeError: 'module' object has no attribute 'pensize'

python是自带turtle还是要单独下载?

我的python是2.7.2

【问题讨论】:

  • 你用的是系统python吗?您发布的代码适用于我的 EPD Python 发行版
  • @mbatchkarov 系统 python 是什么意思?我已经在我的 Mac 上安装了它
  • 我的意思是你的 mac 附带的那个,但这似乎不相关。查看@DSM 给出的答案

标签: python macos turtle-graphics


【解决方案1】:

你调用了你的文件turtle.py,所以import turtle导入你的程序,而不是你想要的模块。

重命名您的程序并删除所有turtle.py[co] 文件。

【讨论】:

  • 我重命名了文件,结果还是一样
  • 如果你所说的“相同的东西”是指AttributeError: 'module' object has no attribute 'pensize',那么你仍然得到了错误的模块。输入import imp,然后输入print imp.find_module("turtle"),查看它尝试导入的文件。
  • 我知道了,我有另一个名为 turtle.pyc 的文件,它与导入冲突。
【解决方案2】:

使用turtle.py 导致了这里的问题。废弃它并改用它

import turtle
turtlename = turtle.Turtle()
wn = turtle.Screen()

完成此操作后,请使用基本的海龟命令,例如

turtlename.forward(90)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2017-08-09
    • 2023-02-16
    • 2012-09-08
    相关资源
    最近更新 更多