By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,994 Members | 1,166 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,994 IT Pros & Developers. It's quick & easy.

ElementTree in Python 2.5

Zver
P: 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
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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
P: 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.