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

Cro*_*oll 10 python python-import python-3.x

码:

from html.parser import HTMLParser
Run Code Online (Sandbox Code Playgroud)

Traceback(最近一次调用最后一次):

  File "program.py", line 7, in <module>
    from html.parser import HTMLParser
ImportError: No module named 'html.parser'; 'html' is not a package
Run Code Online (Sandbox Code Playgroud)

我叫它 python3 program.py

Python版本:Python 3.4.0

Mar*_*ers 11

您已创建一个名为掩盖标准库包的本地文件html.py.

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

python3 -c "import html; print(html.__file__)"
Run Code Online (Sandbox Code Playgroud)

演示:

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
Run Code Online (Sandbox Code Playgroud)


phi*_*hag 5

您在Python路径中的某个位置有一个文件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
Run Code Online (Sandbox Code Playgroud)

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

# Insert temporarily before the problematic line
import html
print(html.__file__)
Run Code Online (Sandbox Code Playgroud)