Python - same line of code only works a second time?

Sorry, I could not describe my problem much better in the title.

I am trying to learn Python and came across this strange behavior and hoped that 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 the minidom (using its fully qaulified name xml.dom.minidom)
This fails, but if I run the same line again, it will work! 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 successively fails.

+3
source share
4 answers

minidom is a module, so you need

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

, parseString, python, .

+4

apport_python_hook.apport_excepthook() , xml.dom.minidom.

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'
>>>  

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'>
+7

( Python 2.6.1 Snow Leopard).:-) , :

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

. .

0

Ubuntu 9.04 (python 2.6.2). python -v, , . , , Ubuntu/Debian - python .

- import xml.dom.minidom.

0
source

Source: https://habr.com/ru/post/1718598/


All Articles