Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SimpleXMLTreeBuilder in elementtree

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?

like image 667
Shard Avatar asked Jul 01 '09 11:07

Shard


2 Answers

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

like image 82
thom_nic Avatar answered Sep 18 '22 14:09

thom_nic


We ran into this same problem using python version 2.6.4 on CentOS 5.5.

The issue happens when the expat class attempts to load the pyexpat modules, see /usr/lib64/python2.6/xml/parsers/expat.py

Looking inside of /usr/lib64/python2.6/lib-dynload/, I didn't see the "pyexpat.so" shared object. However, I did see it on another machine, which wasn't having the problem.

I compared the python versions (yum list 'python*') and identified that the properly functioning machine had python 2.6.5. Running 'yum update python26' fixed the issue for me.

If that doesn't work for you and you want a slapstick solution, you can copy the SO file into your dynamic load path.

like image 41
Ben Avatar answered Sep 22 '22 14:09

Ben