Python Forum

Full Version: Unable to find an existing module
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everybody

I coded this script
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import io
import xml.etree.ElementTree as et

def affiche(noeud, niveau):
	html='<'+noeud.tag
	if niveau > 0:
		html='\t' * int(niveau) + html
	liste=[html]
	for attr in noeud.items():
		x = attr[0] + "='" + attr[1] + "'"
		liste.append(x)
	code_html=' '.join(liste)+">"
	texte_inclus=noeud.text
	try:
		texte_inclus=texte_inclus[:len(texte_inclus)-2]
	except:
		texte_inclus=""
	if len(texte_inclus)>0:
		code_html=code_html+texte_inclus
	print (code_html)
	for fils in noeud.findall('./*'):
		affiche(fils,niveau+1)
	html='</'+noeud.tag+">"
	if niveau > 0:
		html='\t' * int(niveau) + html
	print(html)
	return True
fichier='/home/grec/dictionnaires/bailly/sommaire.xhtml'
racine=et.parse(fichier).getroot()
affiche(racine,0)
When I enter a command to execute it, I receive an error
Error:
remi@remi-Vostro-3550:~$ python '/home/remi/Documents/programmation/python/affiche.py' Traceback (most recent call last): File "/home/remi/Documents/programmation/python/affiche.py", line 5, in <module> import xml.etree.ElementTree as et File "/home/remi/Documents/psilos/programmation/python/xml.py", line 3, in <module> import sys, io, xml.etree.ElementTree as et ModuleNotFoundError: No module named 'xml.etree'; 'xml' is not a package Error in sys.excepthook: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook from apport.fileutils import likely_packaged, get_recent_crashes File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module> from apport.report import Report File "/usr/lib/python3/dist-packages/apport/report.py", line 15, in <module> import xml.dom, xml.dom.minidom File "/home/remi/Documents/psilos/programmation/python/xml.py", line 3, in <module> import sys, io, xml.etree.ElementTree as et ModuleNotFoundError: No module named 'xml.etree'; 'xml' is not a package Original exception was: Traceback (most recent call last): File "/home/remi/Documents/programmation/python/affiche.py", line 5, in <module> import xml.etree.ElementTree as et File "/home/remi/Documents/psilos/programmation/python/xml.py", line 3, in <module> import sys, io, xml.etree.ElementTree as et ModuleNotFoundError: No module named 'xml.etree'; 'xml' is not a package remi@remi-Vostro-3550:~$
However, when I type the «import» command, Python finds xml
remi@remi-Vostro-3550:~$ python
Python 3.8.0 (default, Oct 28 2019, 16:14:01) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import io
>>> import xml.etree.ElementTree as et
>>> sys.path[0]='/home/remi/Documents/programmation/python'
>>> import affiche
<{http://www.w3.org/1999/xhtml}html>
	<{http://www.w3.org/1999/xhtml}head>
		<{http://www.w3.org/1999/xhtml}meta charset='UTF-8'>
		</{http://www.w3.org/1999/xhtml}meta>
		<{http://www.w3.org/1999/xhtml}link rel='stylesheet' type='text/css' href='file:///home/grec/Textes/textes_xml.css'>
		</{http://www.w3.org/1999/xhtml}link>
		<{http://www.w3.org/1999/xhtml}title>Sommaire du bail
		</{http://www.w3.org/1999/xhtml}title>
	</{http://www.w3.org/1999/xhtml}head>
	<{http://www.w3.org/1999/xhtml}body lang='fr-FR' dir='ltr'>
Thank you to anyone who will explain me what's going wrong.

Arbiel
by any chance, do you have xml.py file?

You have xml.py file at /home/remi/Documents/psilos/programmation/python/xml.py
rename it to something else
Hi buran

You are right.

Thank you.

Arbiel