我开始关注本网站上的 elementtree 教程http://www.bigfatalien.com/?p=223所以像往常一样,我在解释器上输入了参考脚本,然后我去了
import xml.etree.ElementTree as xml
解释器可以很好地运行该命令,并且在解释器中使用“xml”没有问题,我的 IDE 甚至在自动完成时显示了该类的成员,但是只要我在脚本上键入完全相同的行并尝试运行它,它说
对象没有属性“etree”
,这条线有效:
import xml
但如果我添加:
xml.etree.ElementTree = xml
并尝试运行那个不起作用的脚本,我尝试使用我的 IDE(pyscripter)和 IDLE,同样的行为。这里发生了什么,这是什么行为,我从未在任何“如何在 python 中导入”教程或书籍中阅读过它。我觉得我错过了一些明显的东西。
使用请求的错误消息进行更新
从 IDLE 2.6 运行提供的行
2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]
[u'C:\\Users\\grillermo\\Desktop', 'C:\\Program Files\\PyScripter\\Lib\\rpyc-python2x.zip', 'C:\\Python26\\lib\\site-packages\\dropbox_client-1.0-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\simplejson-2.1.6-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\poster-0.8.1-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\oauth-1.0.1-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\nose-1.0.0-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\mechanize-0.2.5-py2.6.egg', 'C:\\Windows\\system32\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26', 'C:\\Python26\\lib\\site-packages', 'C:\\Python26\\lib\\site-packages\\win32', 'C:\\Python26\\lib\\site-packages\\win32\\lib', 'C:\\Python26\\lib\\site-packages\\Pythonwin', 'C:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']
2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]
[u'C:\\Users\\grillermo\\Desktop', 'C:\\Program Files\\PyScripter\\Lib\\rpyc-python2x.zip', 'C:\\Python26\\lib\\site-packages\\dropbox_client-1.0-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\simplejson-2.1.6-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\poster-0.8.1-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\oauth-1.0.1-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\nose-1.0.0-py2.6.egg', 'C:\\Python26\\lib\\site-packages\\mechanize-0.2.5-py2.6.egg', 'C:\\Windows\\system32\\python26.zip', 'C:\\Python26\\DLLs', 'C:\\Python26\\lib', 'C:\\Python26\\lib\\plat-win', 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26', 'C:\\Python26\\lib\\site-packages', 'C:\\Python26\\lib\\site-packages\\win32', 'C:\\Python26\\lib\\site-packages\\win32\\lib', 'C:\\Python26\\lib\\site-packages\\Pythonwin', 'C:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']
Traceback (most recent call last):
File "C:\Users\grillermo\Desktop\xml.py", line 4, in <module>
import xml.etree.ElementTree as et
File "C:\Users\grillermo\Desktop\xml.py", line 4, in <module>
import xml.etree.ElementTree as et
ImportError: No module named etree.ElementTree
口译员
C:\>python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as et
>>> print et.__file__
C:\Python26\lib\xml\etree\ElementTree.pyc
>>>