ImportError: No module named 'html.parser'; 'html' is not a package (python3)

ImportError: No module named 'html.parser'; 'html' is not a package (python3)

代码:

from html.parser import HTMLParser

回溯(最近调用最后):

  File "program.py", line 7, in <module>
    from html.parser import HTMLParser
ImportError: No module named 'html.parser'; 'html' is not a package

我用python3 program.py

来称呼它

Python版本:Python3.4.0

您已经创建了一个名为 html.pylocal 文件,它屏蔽了标准库包。

重命名或删除;你可以找到它:

python3 -c "import html; print(html.__file__)"

演示:

naga:Whosebug-3.4 mpieters$ touch html.py
naga:Whosebug-3.4 mpieters$ bin/python -c 'from html.parser import HTMLParser'
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'html.parser'; 'html' is not a package
naga:Whosebug-3.4 mpieters$ bin/python -c "import html; print(html.__file__)"
/.../Whosebug-3.4/html.py
naga:Whosebug-3.4 mpieters$ rm html.py 
naga:Whosebug-3.4 mpieters$ bin/python -c 'from html.parser import HTMLParser; print("Succeeded")'
Succeeded

您的Python path中某处有一个文件html.py(或html.pyc):

$ touch html.py
$ python3 -c 'import html.parser'
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'html.parser'; 'html' is not a package

只需重命名文件(至myhtml.py)。如果你不确定它在哪里,你可以打印它的位置

# Insert temporarily before the problematic line
import html
print(html.__file__)