Error getting Python script to run on older version

时间:2015-04-23 05:24:36

标签: python xml elementtree

I had tested a web based application which uses Python (2.7.8) scripts to create a dynamic content.

I am parsing some XML data and performing several operations on it.

One of the operations requires searching certain type of data which uses the findall(xpath) function from ElementTree.

Now the place where I need to deploy is a Linux box which has an older version of Python (2.6.6).

I had some trouble due to the older ElementTree not having certain capabilities.

I followed this ElementTree SyntaxError: expected path separator ([). I copied the ElementTree from 2.7.8 to the new box and imported from there.

It imports fine but now I get a different error which is as follows:

Traceback (most recent call last):
  File "./filename_python.py", line 117, in <module>
    for ch in child.findall(xpath):
  File "/usr/lib64/python2.6/xml/etree/ElementPath.py", line 293, in findall
    return list(iterfind(elem, path, namespaces))
  File "/usr/lib64/python2.6/xml/etree/ElementPath.py", line 194, in select
    if "".join(e.itertext()) == value:
AttributeError: itertext

I cannot update the Python version on that box and I cannot add any modules so anything new has to be imported locally.

Any help is appreciated.

Thank you.

1 个答案:

答案 0 :(得分:0)

It doesn't seem to me that you've backported the 2.7.x version library properly. Take a look at the stack trace and you will see it is still referring to the library in your Python 2.6.x path:

/usr/lib64/python2.6/xml/etree/ElementPath.py

You could also consider lxml - you don't need to install it as a system library. Simply clone from the git repository here, and copy the code into your projects directory.