Python-无法导入名称视图键

2024-05-23 21:32:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在导入一个inturn导入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


Tags: infrompyimportpackageslinelibrarysite
3条回答

我也有同样的问题:

> 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错误没有回来。

导入inputstream的问题似乎是xhtml2pdf中的一个bug:
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
>>>  

这就是我在MacBook Pro、OS X Yosemite、10.10.5上的帮助

1)检查Python使用的是哪六个版本

import six
print six.__version__

1.4.0

2)查找系统上的所有六个Python包

sudo find / -name 'six*'

/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/lib/six.py
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/lib/six.pyc
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.pyc

3)逐一检查六个版本

vi /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py

4)当文件打开时,向下滚动以识别六个版本

5)手动删除所有早于1.10.0的包

sudo rm /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py
sudo rm /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.pyc

6)安装最新的6个软件包(如1.11.0):

sudo pip install --ignore-installed six

对我来说,PIP不工作,所以我无法重新安装6。

我所做的是:

从这里下载最新的“六”包: https://pypi.python.org/pypi/six

cd /tmp
wget [GET LATEST SIX URL]
tar xf six-1.10.0.tar.gz
cd six-1.10.0
python3 setup.py install

现在PIP在python3中工作,如果python2需要PIP,只需在最后一个命令中将“python3”改为“python2”。

相关问题 更多 >