为什么出现 Python ImportError: 没有名为 html_parser 的模块?

6 投票
2 回答
6153 浏览
提问于 2025-05-01 01:21

我上周把我的电脑升级到了Yosemite。现在,当我尝试运行pelican的开发服务器时,出现了一个导入错误:

$ ./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

我在REPL中直接导入时也遇到了同样的错误,但这个模块其实是已经安装好的:

$ /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>
>>> 

我是不是漏掉了什么明显的东西?这是怎么回事呢?

暂无标签

2 个回答

0

对我来说,这个方法有效:

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

html.unescape(myString)

这样做可以确保Python2的向下兼容性。

5

six这个库使用了一些比较高级的导入技巧。导入html_parser需要用一种特别的方式。通常可以用from six.moves import html_parser来导入。如果你有第三方的代码尝试用其他方式从six导入,你可以先尝试导入six和/或six.moves,然后再导入其他东西。

撰写回答