Reputation: 3171
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.
Upvotes: 3
Views: 2739
Reputation: 32626
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
Upvotes: 5
Reputation: 414079
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'>
Upvotes: 7
Reputation: 53310
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
.
Upvotes: 0
Reputation: 198517
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.
Upvotes: 0