471,596 Members | 926 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

ElementTree in Python 2.5

Zver
2
Hey All!

I'm developing application that uses ElementTree for XML parsing. In python 2.5 ElementTree is part of standard "libs".

Now my question is. How would you detect version of python and then include the right ElementTree.

Psevdo-code :) ->
Expand|Select|Wrap|Line Numbers
  1. If python version < 2.4 then
  2. import elementtree.ElementTree as et
  3. else
  4. import xml.etree.ElementTree as et
  5.  
How to?
Dec 3 '07 #1
2 1575
bvdet
2,851 Expert Mod 2GB
Hey All!

I'm developing application that uses ElementTree for XML parsing. In python 2.5 ElementTree is part of standard "libs".

Now my question is. How would you detect version of python and then include the right ElementTree.

Psevdo-code :) ->
Expand|Select|Wrap|Line Numbers
  1. If python version < 2.4 then
  2. import elementtree.ElementTree as et
  3. else
  4. import xml.etree.ElementTree as et
  5.  
How to?
You can check your Python version like this:
Expand|Select|Wrap|Line Numbers
  1. >>> import sys
  2. >>> sys.version
  3. '2.3.5 (#62, Feb  8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)]'
  4. >>> sys.version_info
  5. (2, 3, 5, 'final', 0)
  6. >>> sys.version_info[:2]
  7. (2, 3)
  8. >>> sys.version_info[:2] == (2,3)
  9. True
  10. >>> 
Dec 3 '07 #2
Zver
2
Final solution for this problem is possibly this:

Expand|Select|Wrap|Line Numbers
  1. try:
  2.  
  3.     import elementtree.ElementTree as et
  4.  
  5. except:
  6.  
  7.     try:
  8.  
  9.         import xml.etree.ElementTree as et
  10.  
  11.     except:
  12.  
  13.         print "No ElementTree in your Python!"
  14.  
Dec 4 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Greg Wilson | last post: by
9 posts views Thread by Chris Spencer | last post: by
1 post views Thread by mirandacascade | last post: by
28 posts views Thread by doug.bromley | last post: by
15 posts views Thread by Steven Bethard | last post: by
reply views Thread by Greg Aumann | last post: by
2 posts views Thread by mirandacascade | last post: by
5 posts views Thread by saif.shakeel | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.