<p>我也有同样的问题:</p>
<pre><code>> python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import six
>>> import xhtml2pdf.pisa
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/xhtml2pdf/pisa.py", line 3, in <module>
from xhtml2pdf.document import pisaDocument
File "/Library/Python/2.7/site-packages/xhtml2pdf/document.py", line 2, in <module>
from xhtml2pdf.context import pisaContext
File "/Library/Python/2.7/site-packages/xhtml2pdf/context.py", line 23, in <module>
import xhtml2pdf.parser
File "/Library/Python/2.7/site-packages/xhtml2pdf/parser.py", line 17, in <module>
from html5lib import treebuilders, inputstream
File "/Library/Python/2.7/site-packages/html5lib/__init__.py", line 16, in <module>
from .html5parser import HTMLParser, parse, parseFragment
File "/Library/Python/2.7/site-packages/html5lib/html5parser.py", line 2, in <module>
from six import with_metaclass, viewkeys, PY3
ImportError: cannot import name viewkeys
>>> exit()
</code></pre>
<p>我运行了以下步骤:<br/></p>
<ul>
<li><code>sudo -H pip uninstall six</code></li>
<li><code>sudo -H pip install six==1.9.0</code></li>
<li>测试:错误仍然存在</li>
<li><code>sudo -H pip uninstall six==1.9.0</code></li>
<li><code>sudo -H pip install six==1.10.0</code></li>
</ul>
<p>测试:</p>
<pre><code>> python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from six import viewkeys
>>> import xhtml.pisa
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named xhtml.pisa
>>> import xhtml2pdf.pisa
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "xhtml2pdf/pisa.py", line 3, in <module>
from xhtml2pdf.document import pisaDocument
File "xhtml2pdf/document.py", line 2, in <module>
from xhtml2pdf.context import pisaContext
File "xhtml2pdf/context.py", line 23, in <module>
import xhtml2pdf.parser
File "xhtml2pdf/parser.py", line 17, in <module>
from html5lib import treebuilders, inputstream
ImportError: cannot import name inputstream
>>> exit()
</code></pre>
<p>所以,viewkeys错误没有回来。</p>
<p>导入inputstream的问题似乎是xhtml2pdf中的一个bug:<br/>
<a href="https://github.com/xhtml2pdf/xhtml2pdf/issues/318">https://github.com/xhtml2pdf/xhtml2pdf/issues/318</a></p>
<p>对我来说,这解决了问题:<br/>
<code>sudo -H pip install html5lib==1.0b8</code></p>
<p>所以,毕竟,我不知道最后一个命令是否能解决整个问题,但现在我可以这么做了:</p>
<pre><code>> python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xhtml2pdf.pisa
>>>
</code></pre>