【问题标题】:swampy.TurtleWorld not working in python 3.4swampy.TurtleWorld 在 python 3.4 中不起作用
【发布时间】:2015-04-23 01:21:11
【问题描述】:

我目前正在使用 ThinkPython 书学习 python,正在使用 python 3.4 和 Anaconda IDE。我需要继续安装一个名为 swampy 的模块。我使用 pip 安装了它,效果很好。导入模块也与 tkinter 一起工作,但我不能使用模块中的任何功能。我检查了我的 lib 文件夹,swampy 在那里,功能也在 swampy 文件夹中。我不知道为什么它不起作用。请我真的需要帮助。如果问题不够清楚,请告诉我。我已经包含了我尝试运行的代码以及每次尝试运行它时收到的错误消息

我尝试运行的代码(第 29 页,think Python the version for python 3.4 的第 4 章)

import tkinter
import swampy
world = swampy.TurtleWorld
bob = Turtle()
print(bob)
wait_for_user()

我收到的错误消息

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Mbaka1\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)
  File "C:\Users\Mbaka1\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
  File "C:/Users/Mbaka1/Documents/Python Scripts/test.py", line 28, in <module>
    world = swampy.TurtleWorld
AttributeError: 'module' object has no attribute 'TurtleWorld'

【问题讨论】:

  • wait_for_user 告诉 TurtleWorld 等待用户做某事,尽管在这种情况下用户除了关闭窗口外没有什么可做的。书上就是这么说的。我检查了 wait_for_user.py 的沼泽文件夹,没有看到任何。

标签: python python-3.4


【解决方案1】:

如果您已下载源代码,本书会显示这些说明:

from TurtleWorld import *
world = TurtleWorld()
bob = Turtle()
print(bob)
wait_for_user()

如果您想在使用 pip 安装后运行代码,这应该可以:

from swampy.TurtleWorld import *
world = TurtleWorld()
bob = Turtle()
print(bob)
wait_for_user()

您所做的事情不起作用的原因是因为TurtleWorldswampy 包中的一个模块,其中包含一个同名的函数,即TurleWorld。因此,当您执行import swampy 然后尝试调用swampy.TurtleWorld 时,您是在尝试调用模块而不是函数。

【讨论】:

  • 上面的代码可以正常工作,但我无法安装 tkinter 模块。我不断收到错误消息could not find a version that can satisfy the requirement Tkinter (from versions) No matching distribution found for Tkinter 非常感谢,确实 TurtleWorld 是沼泽模块中的一个模块。将尝试找到安装 Tkinter 的方法
【解决方案2】:

或者,您可以在此处下载 Think Python 的第二版:http://greenteapress.com/wp/think-python-2e/ 它使用 Python3,并且您不需要 swampy 包来运行此处给出的示例,因为 turtle 和 tkinter(在此处使用)是其中的一部分Python 标准库。

【讨论】:

    【解决方案3】:

    我目前也在研究那本书。我通过添加解决了这个问题:

    import swampy.TurtleWorld
    

    确保 swampy 模块中的 TurtleWorld 模块在您的 shell 中运行。只要您的 Python 版本为 3.4 或 3.5,此功能就可以使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 2014-09-19
      • 2014-08-12
      • 2018-08-18
      • 2015-03-20
      • 2022-11-02
      • 1970-01-01
      相关资源
      最近更新 更多