본문 바로가기
파이썬

파이썬 Python 2.5.4-ImportError : etree.ElementTree라는 모듈이 없습니다.

by º기록 2020. 11. 23.
반응형

Windows에서 Python 2.5.4를 실행 중이며 ElementTree 또는 cElementTree 모듈을 가져 오려고 할 때 계속 오류가 발생합니다. 코드는 매우 간단합니다 (튜토리얼을 따르고 있습니다).

import xml.etree.ElementTree as xml

root = xml.Element('root')
child = xml.Element('child')
root.append(child)
child.attrib['name'] = "Charlie"
file = open("test.xml", 'w')
xml.ElementTree(root).write(file)
file.close()

cmd에서 실행하거나 Python 인터프리터에서 직접 시도하면 오류 메시지가 표시되지 않습니다.

Traceback (most recent call last):  
File "C:\xml.py", line 31, in <module>
  import xml.etree.ElementTree as xml   
File "C:\xml.py", line 31, in <module>
  import xml.etree.ElementTree as xml
ImportError: No module named etree.ElementTree

또한 확인했고 모듈은 C : \ Python25 \ Lib \ xml \ etree에 있습니다.

 

해결 방법

 

튜토리얼에서 매우 중요한 라인을 놓쳤습니다.

import xml.etree.ElementTree as xml

이것은 이제 모듈 전체에서 xml로 알려진 xml.etree.ElementTree를 만듭니다.

나는 파이썬 2.5.4를 가지고 있으며 위와 동일한 코드가 작동하는지 확인했습니다.

user@Comp test$ cat test.py 
import xml.etree.ElementTree as xml

root = xml.Element('root')
child = xml.Element('child')
root.append(child)
child.attrib['name'] = "Charlie"
file = open("test.xml", 'w')
xml.ElementTree(root).write(file)
file.close()

user@Comp test$ /usr/bin/python2.5 --version
Python 2.5.4
user@Comp test$ /usr/bin/python2.5 test.py 
user@Comp test$ cat test.xml 
<root><child name="Charlie" /></root>user@Comp test$ 

따라서 python 2.5.4를 실행하고 있는지 확인하고 다시 설치하십시오. 문제는 파이썬 2.5.4 또는 코드가 아닙니다. 설치 문제이거나 다른 버전의 Python을 실행 중이거나 다른 이상한 문제가 있습니다.

 

참조 페이지 https://stackoverflow.com/questions/3073033

 

 

반응형

댓글