模块在解释器中导入正常,但在脚本中不正常
我开始在这个网站上跟着一个关于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
>>>
2 个回答
当你这样做的时候:
import xml.etree.ElementTree as xml
你是在引入一个叫做 xml.etree.ElementTree
的“对象”,并把它称作 xml
。
但在你第二个版本中似乎不是这样。在那个版本里,你把 xml
作为 xml
导入,然后又反过来赋值:
xml.etree.ElementTree = xml
这看起来是把 ElementTree
对象替换成了顶层的 xml
,这可能不是你想要的结果。我会倾向于说:
xml = xml.etree.ElementTree
这样做会更准确,但我不确定这是否会引起命名上的问题。
无论如何,我看不出你的 import xml.etree.ElementTree as xml
版本到底有什么 错误。它似乎完全符合你的需求(在我的脚本中也运行得很好)。
这是个很简单的问题:你是不是把你的脚本文件命名为 xml.py
呢?如果是的话,千万不要这样做……因为 import xml.anything
会去找你的脚本文件!给脚本文件起一个和你要导入的模块同样的名字,绝对不是个好主意。
更新 错误追踪信息是你的好朋友。一定要仔细阅读它们。如果你不知道怎么读,请把错误追踪信息包含在你的提问中。
我敢打赌,你的错误信息看起来会和这个很相似:
Traceback (most recent call last):
File "xml.py", line 4, in <module>
import xml.etree.ElementTree as et
File "C:\junk\xml.py", line 4, in <module> #### here's the culprit ####
import xml.etree.ElementTree as et
ImportError: No module named etree.ElementTree
否则的话:
创建一个只包含以下几行的脚本:
import sys
print sys.version
print sys.path
import xml.etree.ElementTree as et
print et.__file__
import xml.etree.ElementTree as xml
print xml.__file__
运行它,然后把所有输出结果展示出来……复制粘贴到你问题的编辑中。
在你的电脑上重复以下的解释器会话,并报告打印出的内容:
C:\junk>\python26\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
>>>