Thursday, June 4, 2009

Python ElementTree Fix for Ubuntu

Did you face the following unpleasant situation?

$python
>>> import elementtree
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named elementtree

In case you've installed the elementtree package correctly
(python-elementtree) and you're using Ubuntu,the following
solution will most probably work:

instead of import elementtree, use
import xml.etree.ElementTree

( I believe you must've already figured out the cause of the import
failure and why the solution works.
In case you haven't, read on.
The import failure is due to the directory structure of the
elementtree files being different in Ubuntu. Usually the python
import modules are located in /usr/lib/python
The elementtree import file (ElementTree.py) in Ubuntu however
is located at the path
/usr/lib/python/xml/etree
Hence the above change in the import parameter is necessary )

6 comments:

  1. Thanks! This helped me alot!

    ReplyDelete
  2. See, this right here is why I love the internet and the open source community. Have an odd problem? Someone else has probably had it before you and has posted a solution. Thanks!

    ReplyDelete
  3. Awesome, such a quick fix and so well-explained. Thanks a lot!

    Marcus

    ReplyDelete
  4. This helped me as well, thanks a bunch.

    ReplyDelete
  5. I don't know if anybody still reads this thread,
    but I experience the same error although I try to import the module the same way you described above.

    Are there any other possible problems?

    Thanks for any help!
    Alex

    ReplyDelete
    Replies
    1. I am using Python 2.7.1+

      And here is the code I use:

      import xml.etree.ElementTree as xml

      Error Code:
      Traceback (most recent call last):
      File "/data/pde/matlab/Alex/dtt2mlab/dtt2mlab.py", line 41, in ?
      import xml.etree.ElementTree# as xml
      ImportError: No module named etree.ElementTree
      ??? Error using ==> importdata
      Unable to open file.

      Delete