18

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Croll
  • 3,631
  • 6
  • 30
  • 63
  • I had "ModuleNotFoundError: No module named 'html.parser'" with PyInstaller but not the other part of the error. Therefore, to help others finding this, In the case that you are only getting that error and not the second part about it not being a package, the solution for me was to import the submodules in your progam.py file (or whatever file is in the first argument of your Analysis call in your spec file) and not just in some other module: `import html.parser` and `import html.entities`. These lines in a file that PyInstaller analyzes ensures that PyInstaller will include the submodules. – Poikilos Nov 28 '22 at 06:33

2 Answers2

17

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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • This also happens if you have a file called `html/__init__.py` . – vy32 Jan 05 '21 at 15:53
  • I face the same error as the author and I have a file called html/__init__.py . Do i remove it or rename it? is it a system file? – snow Nov 03 '22 at 09:23
  • @snow: it depends. What does `import html; print(html.__file__)` output? If it is in a path with `lib/python3.9/html` in it (with `3.9` replaced by the actual Python version), then you haven't found the right one. Use the `print()` statement *in the module or script that you have this issue with*. It'll tell you what is being imported instead. – Martijn Pieters Nov 25 '22 at 14:35
6

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__)
phihag
  • 278,196
  • 72
  • 453
  • 469