模块导入适用于解释器,但不适用于脚本
我开始遵循这个网站中关于 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 中导入”教程或书籍中提到的内容。我觉得我错过了一些明显的东西。
使用请求的错误消息更新
提供的行
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
从 IDLE 2.6解释器运行
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
>>>
i was beggining to follow a tutorial on elementtree in this site http://www.bigfatalien.com/?p=223 so as usual i typed the reference scripts on the interpreter and i went
import xml.etree.ElementTree as xml
And the interpreter run that command just fine and using "xml" in the intrepreter with no problems my IDE even showed the members of that class on the autocomplete but as soon as i typed the exact same line on a script and tried to run it, it say
object has no attribute 'etree'
, this line works:
import xml
But if i add:
xml.etree.ElementTree = xml
And try to run that script that doesnt work, i tried with my IDE (pyscripter) and with IDLE, same behaviour.
Whats going on here, whats this behavior, i never read it mentioned on any of the "how to import in python" tutorials or books. I feel i'm missing something obvious.
Update with the error messages requested
Ran the provided lines from 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
The interpreter
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
>>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个非常愚蠢的问题:您是否将脚本文件命名为
xml.py
?如果是这样,请不要这样做...import xml.anything
将在您的脚本文件中查找!为脚本文件提供与要导入的模块相同的名称绝不是一个好主意。更新回溯是你的朋友。导致仔细阅读它们。如果您不知道如何阅读它们,请在您的问题中包含回溯。
我敢打赌你的看起来会与此非常相似:
否则:
设置一个仅包含以下几行的脚本:
运行它,并显示所有输出...复制/粘贴到你的问题的编辑中。
在计算机上重复以下解释器会话,并报告打印的内容:
A really silly question: did you name your script file
xml.py
? If so, DON'T DO THAT ...import xml.anything
will be looking in your script file! It's never a good idea to give a script file the same name as a module that you are going to import.Update Tracebacks are your friends. Lead to read them carefully. If you don't know how to read them, include the traceback in your question.
I'm betting that yours will look very similar to this:
Otherwise:
Set up a script containing only the following lines:
run it, and show all the output ... copy/paste into an edit of your question.
Repeat the following interpreter session on your computer, and report what is printed:
当您这样做时:
您将引入
xml.etree.ElementTree
“对象”并将其命名为xml
。您的第二个版本中似乎并非如此。在这种情况下,您将
xml
导入为xml
,然后分配错误的方式:这似乎是用对的引用替换
ElementTree
对象顶级xml
可能不是您想要的。我很想说:这是更准确的方法,但我不确定这是否会导致命名问题。
无论如何,我看不出您的
import xml.etree.ElementTree as xml
版本实际上有什么错误 它似乎完全符合您的要求(并且在为我编写的脚本)。When you do:
you are bringing in the
xml.etree.ElementTree
"object" and calling itxml
.That doesn't appear to be the case in your second version. In that, you import the
xml
asxml
and then assign the wrong way around:That appears to be replacing the
ElementTree
object with a reference to the top-levelxml
, probably not what you wanted. I'd be tempted to say that:would be the more accurate way but I'm not sure if that's going to cause problems with naming.
In any case, I can't see what's actually wrong with your
import xml.etree.ElementTree as xml
version It appears to do exactly what you want (and works fine in scripts for me).