Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Same line of code only works the second time it called?

Tags:

python

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.

like image 317
occhiso Avatar asked Sep 13 '26 05:09

occhiso


1 Answers

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'>
like image 199
jfs Avatar answered Sep 15 '26 19:09

jfs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!