Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import from Python six library

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.

like image 896
Ron Cohen Avatar asked May 21 '16 16:05

Ron Cohen


2 Answers

This question seems to be answered by this one (I don't know how to mark this one as duplicate).

So unfortunately there's no easy solution because of the dynamic nature of the "six" module unless you implement your own python-skeleton for this module (which is not what straithforward).

like image 121
ThoSil Avatar answered Nov 13 '22 13:11

ThoSil


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.

like image 30
Burhan Khalid Avatar answered Nov 13 '22 14:11

Burhan Khalid