uchembaka uchembaka - 4 months ago 15
Python Question

swampy.TurtleWorld not working in python 3.4

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

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()


Error Message i got

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'

Answer

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

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

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()

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.

Comments