반응형
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
반응형
'파이썬' 카테고리의 다른 글
파이썬에서 대괄호의 다른 의미 (0) | 2020.11.23 |
---|---|
파이썬 파이 게임의 카운트 다운 타이머 (0) | 2020.11.23 |
파이썬 Python으로 소리 재생 (0) | 2020.11.23 |
파이썬 Python Nose Import Error (0) | 2020.11.23 |
파이썬 How to filter objects for count annotation in Django? (0) | 2020.11.22 |
댓글