Module import works on interpreter but not on a script
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\\开发者_JAVA百科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
>>>
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:
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
>>>
When you do:
import xml.etree.ElementTree as xml
you are bringing in the xml.etree.ElementTree
"object" and calling it 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.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 = 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).
精彩评论