ImportError:没有名为'html.parser'的模块; 'html'不是包(python3)

时间:2015-03-05 11:27:12

标签: python python-3.x python-import

代码:

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版本:Python 3.4.0

2 个答案:

答案 0 :(得分:11)

您已创建名为html.py本地文件,该文件会屏蔽标准库包。

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

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

演示:

naga:stackoverflow-3.4 mpieters$ touch html.py
naga:stackoverflow-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:stackoverflow-3.4 mpieters$ bin/python -c "import html; print(html.__file__)"
/.../stackoverflow-3.4/html.py
naga:stackoverflow-3.4 mpieters$ rm html.py 
naga:stackoverflow-3.4 mpieters$ bin/python -c 'from html.parser import HTMLParser; print("Succeeded")'
Succeeded

答案 1 :(得分:5)

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__)