我正在导入一个模块,该模块又导入了six,但出现了这个奇怪的错误。

Traceback (most recent call last):
  File "/Users/praful/Desktop/got/modules/categories/tests.py", line 13, in <module>
    import microdata
  File "build/bdist.macosx-10.10-intel/egg/microdata.py", line 4, in <module>
  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

我看一下six.py,它里面有viewkeys

已安装最新的six==1.10.0

最佳答案

我有同样的问题:

> 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()

我执行以下步骤:
  • sudo -H pip uninstall six
  • sudo -H pip install six==1.9.0
  • 测试:错误仍然存​​在
  • sudo -H pip uninstall six==1.9.0
  • sudo -H pip install six==1.10.0

  • 测试:
    > 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()
    

    因此,viewkeys-error并没有回来。

    导入inputstream的问题似乎是xhtml2pdf中的错误:
    https://github.com/xhtml2pdf/xhtml2pdf/issues/318

    对我来说,这解决了这个问题:sudo -H pip install html5lib==1.0b8
    因此,毕竟,我真的不知道最后一个命令是否可以整体解决问题,但是这种方式现在对我有用:
    > 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
    >>>
    

    关于Python-无法导入名称 View 键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39658404/

    10-11 00:28