swampy.TurtleWorld 在 python 3.4 中不起作用 [英] swampy.TurtleWorld not working in python 3.4

查看:70
本文介绍了swampy.TurtleWorld 在 python 3.4 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I m currently learning python using the ThinkPython book, am using python 3.4 and the Anaconda IDE. Part of what I need to continue is to install a module called swampy. I installed it using pip, which worked very well. Importing the module worked too together with tkinter, but I can't use any of the functions in the module. I checked my lib folder, swampy is there and the functions too are in the swampy folder. I can't figure out why its not working. Please I really need help. If the question isn't clear enough please let me know. I have included the code i tried to run and the error message I get each time I try running it

我尝试运行的代码(第 29 页,第 4 章认为 Python 是 Python 3.4 的版本)

The code i try to run (page 29, Chapter 4 of think Python the version for python 3.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'

推荐答案

如果您下载了源代码,本书会显示以下说明:

The book shows these directions if you've downloaded the source code:

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

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

If you want to run the code after installing with pip, this should work:

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

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

The reason what you're doing isn't working is because TurtleWorld is a module within the swampy package, which contains a functions with the same name, i.e. TurleWorld. So when you do import swampy and then try calling swampy.TurtleWorld you're trying to call a module rather than the function.

这篇关于swampy.TurtleWorld 在 python 3.4 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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