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
我正在开始关注这个网站http://www.bigfatalien.com/?p=223上关于elementtree的教程,所以像往常一样,我在解释器上输入了参考脚本,然后我去了
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
解释器运行该命令就好并在intrepreter中使用“xml”没有问题我的IDE甚至在自动完成时显示该类的成员但是只要我在脚本上键入完全相同的行并尝试运行它,它说
object has no attribute 'etree'
对象没有属性'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.
并尝试运行那个不起作用的脚本,我尝试使用我的IDE(pyscripter)和IDLE,同样的行为。什么事情发生在这里,这是什么行为,我从来没有读过任何“如何导入python”教程或书籍。我觉得我错过了一些明显的东西。
Update with the error messages requested
使用请求的错误消息进行更新
Ran the provided lines from IDLE 2.6
从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
>>>
2 个解决方案
#1
12
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.
一个非常愚蠢的问题:你是否将脚本文件命名为xml.py?如果是这样,请不要这样...导入xml.anything将查找您的脚本文件!为脚本文件提供与要导入的模块相同的名称绝不是一个好主意。
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:
我打赌你的看起来非常相似:
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
Otherwise:
除此以外:
Set up a script containing only the following lines:
设置仅包含以下行的脚本:
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__
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:
在您的计算机上重复以下解释器会话,并报告打印的内容:
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
>>>
#2
1
When you do:
当你这样做时:
import xml.etree.ElementTree as xml
you are bringing in the xml.etree.ElementTree
"object" and calling it xml
.
你引入了xml.etree.ElementTree“对象”并将其称为xml。
That doesn't appear to be the case in your second version. In that, you import the xml
as xml
and then assign the wrong way around:
在您的第二个版本中似乎并非如此。在那里,您将xml导入为xml,然后分配错误的方法:
xml.etree.ElementTree = xml
That appears to be replacing the ElementTree
object with a reference to the top-level xml
, probably not what you wanted. I'd be tempted to say that:
这似乎是用对*xml的引用替换ElementTree对象,可能不是你想要的。我很想说:
xml = xml.etree.ElementTree
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).
在任何情况下,我都看不出你的导入xml.etree.ElementTree作为xml版本实际上有什么问题它似乎完全符合你的要求(并且在我的脚本中工作正常)。
#1
12
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.
一个非常愚蠢的问题:你是否将脚本文件命名为xml.py?如果是这样,请不要这样...导入xml.anything将查找您的脚本文件!为脚本文件提供与要导入的模块相同的名称绝不是一个好主意。
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:
我打赌你的看起来非常相似:
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
Otherwise:
除此以外:
Set up a script containing only the following lines:
设置仅包含以下行的脚本:
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__
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:
在您的计算机上重复以下解释器会话,并报告打印的内容:
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
>>>
#2
1
When you do:
当你这样做时:
import xml.etree.ElementTree as xml
you are bringing in the xml.etree.ElementTree
"object" and calling it xml
.
你引入了xml.etree.ElementTree“对象”并将其称为xml。
That doesn't appear to be the case in your second version. In that, you import the xml
as xml
and then assign the wrong way around:
在您的第二个版本中似乎并非如此。在那里,您将xml导入为xml,然后分配错误的方法:
xml.etree.ElementTree = xml
That appears to be replacing the ElementTree
object with a reference to the top-level xml
, probably not what you wanted. I'd be tempted to say that:
这似乎是用对*xml的引用替换ElementTree对象,可能不是你想要的。我很想说:
xml = xml.etree.ElementTree
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).
在任何情况下,我都看不出你的导入xml.etree.ElementTree作为xml版本实际上有什么问题它似乎完全符合你的要求(并且在我的脚本中工作正常)。