Ron Cohen
Ron Cohen

Reputation: 2925

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

_

$ 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.

Upvotes: 7

Views: 12541

Answers (3)

Shaohan Huang
Shaohan Huang

Reputation: 295

You can use import six instead of from six.moves import cPickle, then use six.moves.cPickle.

It works for me.

Upvotes: 0

ThoSil
ThoSil

Reputation: 134

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

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174692

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.

Upvotes: 1

Related Questions