tags:

views:

221

answers:

4

Sorry I couldn't really describe my problem much better in the title.

I am trying to learn Python, and came across this strange behavior and was hoping someone could explain this to me.

I am running Ubuntu 8.10 and python 2.5.2

First I import xml.dom
Then I create an instance of a minidom (using its fully qaulified name xml.dom.minidom)
This fails, but then if I run that same line again, it works! See below:

$> python
Python 2.5.2 (r252:60911, Oct  5 2008, 19:29:17) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.dom
>>> xml.dom.minidom.parseString("<xml><item/></xml>")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'minidom'
>>> xml.dom.minidom.parseString("<xml><item/></xml>")
<xml.dom.minidom.Document instance at 0x7fd914e42fc8>

I tried on another machine, and if consistently fails.

+4  A: 

minidom is a module so you should need

import xml.dom.minidom
xml.dom.minidom.parseString("<xml><item/></xml>")

I don't know how you got the second parseString to work it fails on my python as in your other machine

Mark
Thanks, Yea, I wanted access to xml.dom.Node.* so I wanted to import the package from a higher level.I guess the way to do that would be:import xml.domimport xml.dom.minidom.. that seems to work, I guess thats normal?
occhiso
A: 

I couldn't get your code to work even on the second try (using Python 2.6.1 on Snow Leopard). :-) However, here's one version that does work for me:

>>> from xml.dom.minidom import parseString
>>> parseString("<xml><item/></xml>")
<xml.dom.minidom.Document instance at 0x100539830>

Personally, I prefer this style of import. It tends to make for much less verbose code.

Jason Baker
A: 

I can replicate your behaviour on Ubuntu 9.04 (python 2.6.2). If you do python -v you can see the first error causes lots of extra imports. Since it doesn't happen for everybody, I can only assume the Ubuntu/Debian have added something to python to auto load modules.

Still the recommended action is to import xml.dom.minidom.

Douglas Leeder
+6  A: 

The problem is in apport_python_hook.apport_excepthook() as a side effect it imports xml.dom.minidom.

Without apport_except_hook:

>>> import sys
>>> sys.excepthook = sys.__excepthook__
>>> import xml.dom
>>> xml.dom.minidom
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'minidom'
>>> xml.dom.minidom
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'minidom'
>>>

With apport_except_hook:

>>> import apport_python_hook
>>> apport_python_hook.install()
>>> xml.dom.minidom
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'minidom'
>>> xml.dom.minidom
<module 'xml.dom.minidom' from '../lib/python2.6/xml/dom/minidom.pyc'>
J.F. Sebastian