Cannot import from Python six library

Ron Cohen picture Ron Cohen · May 21, 2016 · Viewed 7k times · Source

Cannot import anything from six. Both of these lines yield errors:

from six.moves.urllib.request import urlretrieve

--> Unresolved reference "urlretrieve"

from six.moves import cPickle as pickle

--> Unresolved reference "cPickle"

OSX 10.11.5. IntelliJ 2016.1.2, PyCharm 2016.1.3, Python 2.7.11, Six 1.10.0.

What I've done to try to fix (per PyCharm shows unresolved references error for valid code and other posts):

  • Switched Python interpreters
  • IntelliJ: deleted the idea file
  • IDE -> Invalidate Caches / Restart
  • Created a completely new project with same source
  • SWitched IDEs (IntelliJ -> PyCharm)
  • Verified six is current:

_

$ sudo pip install -U six
Requirement already up-to-date: six in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six-1.10.0-py2.7.egg

Python SDKs I tried:

Python 2.7.11 (/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7)
Python 3.5.1 (/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5)
Python 2.7.10 (/usr/local/bin/python2.7)

Nothing worked. Help would be welcome! Thanks.

---------- Update May 23 2016 -------------------------------

Running the "from six.moves..." import lines from the command line does not yield errors. The command line invokes Python 2.7.11. PyCharm also is using Python 2.7.11, and PyCharm also shows that six is available - see first screen shot. There are no .pyc files in the project, just main.py. enter image description here Oddly, main.py runs without error within PyCharm; the "Hello" line does print (see second screen shot). File runs OK This screen shot shows the project interpreter in the settings: enter image description here I'm still stumped.

---------- Update May 24 2016 -----------------------------

import six works without error in both the code and the python shell and after execution, six.file = /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six-1.10.0-py2.7.egg/six.pyc. However, the PyCharm IDE still shows the errors in the six.moves import lines.

Answer

Burhan Khalid picture Burhan Khalid · May 21, 2016

six is only installed at /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7, so it will only work if you use that interpreter in PyCharm/IntelliJ

Note, if you create a new virtual environment, you'll have to install six in it separately.

If PyCharm/IntelliJ is giving you these warnings, but the code actually runs - then this is just a caching issue with PyCharm and you can safely ignore it - chances are on the next cache rebuild PyCharm will update its cache.