views:

600

answers:

4

I have been developing an application with django and elementtree and while deploying it to the production server i have found out it is running python 2.4. I have been able to bundle elementtree but now i am getting the error:

"No module named expat; use SimpleXMLTreeBuilder instead"

Unfortunately i cannot upgrade python so im stuck with what i got. How do i use SimpleXMLTreeBuilder as the parser and/or will i need to rewrite code?

A: 

Assuming you're now using elementtree.XMLTreeBuilder, just try this instead:

from elementtree import SimpleXMLTreeBuilder as XMLTreeBuilder

It tries to provide exactly the same functionality but using xmllib instead of expat. If that also fails, BTW, try:

from elementtree import SgmlopXMLTreeBuilder as XMLTreeBuilder

to attempt to use yet another implementation, this one based on sgmlop instead.

Alex Martelli
A: 

from elementtree import SimpleXMLTreeBuilder as XMLTreeBuilder

Ok, it has slightly changed now:

Traceback (most recent call last):
  File "C:\Python26\tests\xml.py", line 12, in <module>
    doc = elementtree.ElementTree.parse("foo.xml")
  File "C:\Python26\lib\site-packages\elementtree\ElementTree.py", line 908, in parse
    tree = parser_api.parse(source)
  File "C:\Python26\lib\site-packages\elementtree\ElementTree.py", line 169, in parse
    parser = XMLTreeBuilder()
  File "C:\Python26\lib\site-packages\elementtree\ElementTree.py", line 1165, in __init__
    "No module named expat; use SimpleXMLTreeBuilder instead"
ImportError: No module named expat; use SimpleXMLTreeBuilder instead

I think it was statically linked with older version of Python or something. Is there an easy way to attach XML parser to Python 2.6? Some libraries seem to only work with older versions 8(

+1  A: 

If you have third party module that wants to use ElementTree (and XMLTreeBuilder by dependency) you can change ElementTree's XMLTreeBuilder definition to the one provided by SimpleXMLTreeBuilder like so:

from xml.etree import ElementTree # part of python distribution
from elementtree import SimpleXMLTreeBuilder # part of your codebase
ElementTree.XMLTreeBuilder = SimpleXMLTreeBuilder.TreeBuilder

Now ElementTree will always use the SimpleXMLTreeBuilder whenever it's called.

See also: http://groups.google.com/group/google-appengine/browse_thread/thread/b7399a91c9525c97

Thom Nichols
A: 

I was getting the same problem. I downloaded the latest element tree package from effbot and ran my script from the element tree folder of the extract. It worked.

Anu