簡體   English   中英

ImportError:沒有名為'html.parser'的模塊; 'html'不是包(python3)

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

碼:

from html.parser import HTMLParser

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

  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

您已創建一個名為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

你的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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM