module error for elementtree
John Machin
sjmachin at lexicon.net
Fri May 11 21:02:09 EDT 2007
On May 11, 11:12 pm, "Jerry Hill" <malaclyp... at gmail.com> wrote:
> On 11 May 2007 00:05:19 -0700, saif.shak... at gmail.com
>
> <saif.shak... at gmail.com> wrote:
> > For the above code to work elementtree is
> > imported in first line ,but when running it says :
> > ImportError: No module named elementtree.ElementTree
> > Does thie module exists as default or a patch is needed?
>
> That depends on which version of Python you are running. Starting
> with 2.5, ElementTree was moved into the standard library, as part of
> the xml package. If you're using an older version of python, I think
> you'll need to download and install the elementtree package fromhttp://effbot.org/zone/element-index.htm
>
> Assuming you're using python 2.5, you probably want something like this:
>
> from xml.etree.ElementTree import ElementTree as et
>
1. The OP appears to be confusing ElementTree and Element (objects of
the first class are containers of objects of the second class).
2. For any serious work to be done efficiently, the cElementTree
module should be used.
3. Here's some code that appears to work for supporting multiple
versions of Python:
8< --- import_et.py ---
import sys
python_version = sys.version_info[:2]
print >> sys.stderr, "Python version:", sys.version_info
if python_version >= (2, 5):
import xml.etree.cElementTree as ET
else:
try:
import cElementTree as ET
except ImportError:
try:
import ElementTree as ET
except ImportError:
msg = "\nYou need the [c]ElementTree package\n" \
"from http://effbot.org/downloads/\n\n"
sys.stderr.write(msg)
raise
print >> sys.stderr, "ET imported from", ET.__file__
8< --- end of import_et.py ---
4. and some (occasionally astonishing) results:
C:\junk>for %v in (5,1,4) do \python2%v\python import_et.py
C:\junk>\python25\python import_et.py
Python version: (2, 5, 1, 'final', 0)
ET imported from C:\python25\lib\xml\etree\cElementTree.pyc
C:\junk>\python21\python import_et.py
Python version: (2, 1, 3, 'final', 0)
ET imported from C:\python21\cElementTree.pyd
C:\junk>\python24\python import_et.py
Python version: (2, 4, 3, 'final', 0)
ET imported from C:\Documents and Settings\sjm\Application Data\Python-
Eggs\celementtree-1.0.5_20051216-py2.4-win32.egg-tmp\cElementTree.pyd
IIRC, I have TurboGears (a leading contender for the "All of your
sys.path are belong to us" award) to thank for that little gem :-)
HTH,
John
More information about the Python-list
mailing list