tags:

views:

121

answers:

4

Then how do I import that? I run everything in python 2.4, but one of my scripts import xml.etree.ElementTree...which is only Python 2.5

+4  A: 

Then it fails.

You can't import a python 2.5 library while you're running python 2.4. It won't work.

Why can't you run python 2.5+?

Paul McMillan
Many of my scripts (Django included) run on Python 2.4, and that is my default.
TIMEX
Django runs just fine on Python 2.6. Why do you need 2.4? There aren't many serious backwards compatibility problems between the two. If you want a 2.5 library, you have to use 2.5+. It's as simple as that.
Paul McMillan
+6  A: 

Consider upgrading to Python 2.5 (or, dare I say, 2.6!-) one of these decades... the times, they are a'changin'! Meanwhile, for ElementTree in particular, you might manage to back-patch it, but...

Come gather 'round people
Wherever you roam
And admit that the waters
Around you have grown
And accept it that soon
You'll be drenched to the bone.
If your time to you
Is worth savin'
Then you better start swimmin'
Or you'll sink like a stone
For the times they are a-changin'.

And if hearing it in great music helps you, feel free... but you'd better start swimmin', or you'll sink like a stone...!-)

Alex Martelli
Alex. What on earth is in the water at Google? I want it.
Jed Smith
@Jed: mostly healthy, good-for-you infusions -- basil, lime, and so forth. But right now I'm at home and here I subscribe to my beloved grandma's mantra (she left us at 96, 15 years ago) -- "water breeds rust, pour me another brandy!-)" -- though right now I'm eschewing plain brandy in favor of Irish Iced Tea, http://www.barnonedrinks.com/drinks/i/irish-iced-tea-12689.html .But -- "the times, they are a-changin'" (and most of the rest of Dylan and other prophets of our generation -- Cohen, Billy Joel, The Boss, etc) is well worth listening to... even for tee-totallers!-)
Alex Martelli
I'll have to try that Irish Iced Tea... I've only had beer yet this evening, but which variety of brandy is preferred?
Paul McMillan
There's something happening, but I don't know what it is.
Robert Rossney
+1  A: 

This type of problem is not all that unusual, as not everyone has control over the version of Python that they use. A few years ago I had this very problem as most of my work took place on a Windows PC, but at one point we needed to run it on a large UNIX box which was subject to strict change control, meaning that I couldn't install Python 2.5. I think even now that box may be running Python 2.4.

Looking back on my solution, it wasn't particularly elegant, but here it is...

while True:
    try:
        from xml.etree.cElementTree import *
        break
    except ImportError:
        pass
    try:
        from xml.etree.ElementTree import *
        break
    except ImportError:
        pass
    try:
        from cElementTree import *
        break
    except ImportError:
        pass
    from ElementTree import *
    break

This was part of a module called xmlutils.py which was used by the rest of my code rather than importing xml.etree.ElementTree (or whatever) directly.

Edit: I should point out that cElementTree and ElementTree were the names for xml.etree.cElementTree and xml.etree.ElementTree before they became part of the standard library.

Rod Hyde
ouch. Why the `*` imports?? `import ElementTree as ET` etc would be better, right?
kaizer.se
@kaizer.se The idea was to bring it all into the namespace of xmlutils so that other modules could to things like "from xmlutils import Element".
Rod Hyde
+2  A: 

You should be able to install ElementTree for Python 2.4. It is in debian under the package name python2.4-elementtree (and ..-celementtree)

If you don't find a package for your OS, you can install it right from Source.

kaizer.se