为什么我得到一个python ImportError:没有名为html_parser的模块?

时间:2014-11-01 18:59:46

标签: python pelican

我上周将我的机器升级为优胜美地。现在,当尝试运行pelican devserver时,我得到一个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

尝试直接导入时,我从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 :(得分:5)

这六个库使用了一些高级导入魔法。 html_parser的导入必须以特殊方式完成。 from six.moves import html_parser通常有用。如果您有第三方代码尝试从另外六种方式导入,您可以先尝试导入six和/或six.moves,然后再导入其他内容。

答案 1 :(得分:0)

为我工作:

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

html.unescape(myString)

这保证了Python2的向下兼容性。