Why am I getting a python ImportError: No module named html_parser?
Asked Answered
N

2

6

I upgraded my machine to Yosemite last week. Now when trying to run the pelican devserver I get an ImportError:

$ ./develop_server.sh start
Starting up Pelican and HTTP server
Traceback (most recent call last):
  File "/usr/local/bin/pelican", line 7, in <module>
    from pelican import main
  File "/Library/Python/2.7/site-packages/pelican/__init__.py", line 20, in <module>
    from pelican.generators import (ArticlesGenerator, PagesGenerator,
  File "/Library/Python/2.7/site-packages/pelican/generators.py", line 23, in <module>
    from pelican.readers import Readers
  File "/Library/Python/2.7/site-packages/pelican/readers.py", line 24, in <module>
    from six.moves.html_parser import HTMLParser
ImportError: No module named html_parser
/usr/bin/python: No module named html_parser
Pelican didn't start. Is the Pelican package installed?
Stale PID, deleting
Stale PID, deleting

I get the same error from the REPL when trying to do a direct import, yet the module is installed:

$ /usr/bin/python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import 
KeyboardInterrupt
>>> from six.moves.html_parser import HTMLParser
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named html_parser
>>> import six
>>> six.moves.html_parser
<module 'HTMLParser' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/HTMLParser.pyc'>
>>> six.moves.html_parser.HTMLParser
<class HTMLParser.HTMLParser at 0x10b751530>
>>> 

Am I missing something obvious? What is going on here?

Normand answered 1/11, 2014 at 18:59 Comment(1)
You might need to reinstall pelicanVersify
D
5

The six library uses some advanced import magic. The import of html_parser has to be done in a special way. from six.moves import html_parser usually works. If you have third party code that tries to import from six in an other way you can try to import six and/or six.moves at first before importing anything else.

Domitiladomonic answered 1/11, 2014 at 20:52 Comment(4)
Thanks, I'll raise this to the pelican team as bugNormand
I confirm that I got the same problem on another project and this kind of import solved the problem.Abominable
I am not using this html_parser in my code but when I make a binary executable out of my code using pyinstaller, it's asking me to import the html_parser.. what could be the reason.. Is it really need html_parser in case we are planning to make binary executable.. Please shred some light.. thanksGerhardt
@LeoPrince Please open a new question with all the necessary information to assist.Domitiladomonic
R
0

Worked for me:

from six.moves import html_parser
html = html_parser.HTMLParser()

html.unescape(myString)

This guaranteed Python2 downward compatibility.

Riggins answered 4/12, 2018 at 0:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.