Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - cannot import name viewkeys

Tags:

python

I'm importing a module which inturn imports six, but I'm getting this weird error.

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

I'd a look at six.py, it has viewkeys in it.

Latest six==1.10.0 is installed.

like image 955
PythonEnthusiast Avatar asked Sep 23 '16 10:09

PythonEnthusiast


2 Answers

I had the same problem:

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

I ran the following steps:

  • sudo -H pip uninstall six
  • sudo -H pip install six==1.9.0
  • Test: Error persists
  • sudo -H pip uninstall six==1.9.0
  • sudo -H pip install six==1.10.0

Test:

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

So the viewkeys-error didn't come back.

The problem importing inputstream seems to be a bug in xhtml2pdf:
https://github.com/xhtml2pdf/xhtml2pdf/issues/318

For me this fixed the problem:
sudo -H pip install html5lib==1.0b8

So afterall, I don't really know if the last command would have fixed the problem overall, but this way it works for me now:

> 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
>>>  
like image 194
sn0b4ll Avatar answered Oct 19 '22 11:10

sn0b4ll


This is what helped me on my MacBook Pro, OS X Yosemite, 10.10.5

1) Check which six version your Python is using

import six
print six.__version__

1.4.0

2) Find all six Python packages on your system

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) Check the six versions one by one

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

4) When the file opens scroll down to identify the six version

5) Manually remove all packages older then 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) Install the latest six package (e.g. 1.11.0):

sudo pip install --ignore-installed six
like image 33
Tony Avatar answered Oct 19 '22 12:10

Tony