Jim*_*yap 11 python python-3.x
我是从这个网站自学python的.在第3章,当我在给定示例中键入代码时,我收到以下错误 -
Python 3.2 (r32:88445, Mar 25 2011, 19:28:28)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "turtle.py", line 2, in <module>
wn = turtle.Screen()
AttributeError: 'module' object has no attribute 'Screen'
>>>
Run Code Online (Sandbox Code Playgroud)
这是我需要下载和安装的东西吗?我试着查看docs.python.org,但是我的鼻子开始流血,阅读所有技术内容.请指出我正确的方向?谢谢.
Joh*_*web 22
Adam Bernier的答案可能是正确的.看起来你有一个名为turtle.pyPython 的文件在Python安装附带之前就已经开始了.
要追查这些问题:
% python
Python 2.7.1 (r271:86832, Jan 29 2011, 13:30:16)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
[...] # Your ${PYTHONPATH}
>>> import turtle
>>> turtle.__file__
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/turtle.pyc' # Should be under your Python installation.
>>>
Run Code Online (Sandbox Code Playgroud)
如果你看到这样的话:
>>> import turtle
>>> turtle.__file__
'turtle.py'
Run Code Online (Sandbox Code Playgroud)
然后,您将要在当前工作目录中移动turtle.py(以及任何相应的turtle.pyc或turtle.pyo文件).
根据下面的评论,您可以通过调用help()来找到有关模块的大量信息,包括其路径名和内容.例如:
>>> import turtle
>>> help(turtle)
Run Code Online (Sandbox Code Playgroud)
ber*_*nie 16
重命名turtle.py.它与导入的同名模块发生冲突.
我测试了该站点的代码在Python 2.6中工作(不安装任何外部包).
来自http://docs.python.org/tutorial/modules.html#the-module-search-path
spam导入命名模块时,解释器将搜索spam.py当前目录中指定的文件,然后搜索环境变量指定的目录列表PYTHONPATH.
所以Python解释器找到你的 turtle.py文件,但没有看到Screen该文件中的类.
Johnsyweb的答案包含几个关于如何调试此类问题的好技巧.确定导入模块所在文件系统所在位置的最直接方法可能是repr(module)在REPL提示符处使用或只是键入模块名称,例如:
>>> turtle
<module 'turtle' from '/usr/lib/python2.6/lib-tk/turtle.pyc'>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31243 次 |
| 最近记录: |