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

25,638

Solution 1

You have created a local file named html.py that masks the standard library package.

Rename it or delete it; you can locate it with:

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

Demo:

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

Solution 2

You have a file html.py (or html.pyc) somewhere in your Python path:

$ 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

Simply rename the file (to myhtml.py). If you are unsure where it is, you can print its location with

# Insert temporarily before the problematic line
import html
print(html.__file__)
Share:
25,638
Croll
Author by

Croll

Updated on July 27, 2022

Comments

  • Croll
    Croll almost 2 years

    Code:

    from html.parser import HTMLParser
    

    Traceback (most recent call last):

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

    I call it with python3 program.py

    Python version: Python 3.4.0

  • Ami
    Ami over 3 years
    This also happens if you have a file called html/__init__.py .