Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 774
Croll Avatar asked Mar 05 '15 11:03

Croll


2 Answers

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
like image 69
Martijn Pieters Avatar answered Oct 16 '22 13:10

Martijn Pieters


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__)
like image 6
phihag Avatar answered Oct 16 '22 14:10

phihag