【问题标题】:ImportError: No module named 'html.parser'; 'html' is not a package (python3) [duplicate]ImportError:没有名为“html.parser”的模块; “html”不是一个包(python3)[重复]
【发布时间】:2015-03-05 11:27:11
【问题描述】:

代码:

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

【问题讨论】:

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


    【解决方案1】:

    您已经创建了一个名为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
    

    【讨论】:

    • 如果您有一个名为 html/__init__.py 的文件,也会发生这种情况。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 2016-01-23
      • 2019-07-07
      • 2018-10-16
      • 2014-07-15
      • 2018-04-12
      • 1970-01-01
      相关资源
      最近更新 更多