问题
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'
回答1:
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.
回答2:
Alternatively, you could download the second edition of Think Python here: http://greenteapress.com/wp/think-python-2e/ It uses Python3 and you do not need the swampy package to run the examples given here, as turtle and tkinter (which are used here) come as part of the Python standard library.
回答3:
I'm currently studying that book, too. I solved this problem by adding:
import swampy.TurtleWorld
to ensure that the TurtleWorld module in the swampy module runs in your shell. This works as long as your Python version is 3.4 or 3.5.
来源:https://stackoverflow.com/questions/29811920/swampy-turtleworld-not-working-in-python-3-4