Cannot import from Python six library
Asked Answered
I

3

7

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.

Iota answered 21/5, 2016 at 16:47 Comment(7)
Are the lines you're showing in code that you're trying to load and run? Have you tried just typing these lines into a python shell?Ajaajaccio
Yes, the import lines above are in code that I want to run. I have not even entered the operational code yet, I was just trying to get all of the imports working first. This code is from a Google Tensorflow tutorial. When I type those lines into a python shell I do not get any error, so maybe these are just warnings that I can ignore as Burhan suggests below?Iota
So, what Burhan is telling you is that you need to run the Python interpreter that knows about six in order to import it. Apparently, that's the one you get when you start a shell from the command line. You need to figure out how to make PyCharm or IntelliJ or whatever tool you want to use run the interpreter that knows about six for the import to work. If it's the caching issue he mentions, the error should go away with a cache rebuild. Try removing files named *.pyc and directories named pycache and see if that helps.Ajaajaccio
Thanks Tom. I tried your suggestions but no change. I added more details and screen shots in the post above. Strange that the file runs but leaves no .pyc file..Iota
Can you do import six? If so, what is the value of six.__file__ after you import it?Sternberg
Yes, "import six" seems to work both in the code and in cmd line python. six.__file__ = '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/six-1.10.0-py2.7.egg/six.pyc'. Printing that value from the code and from Python console both output the same value.Iota
Possible duplicate of Getting six and six.moves modules to autocomplete in pycharmHesperidin
B
1

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

Beaty answered 21/9, 2016 at 15:25 Comment(0)
B
1

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.

Bazil answered 21/5, 2016 at 16:54 Comment(0)
B
1

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

Beaty answered 21/9, 2016 at 15:25 Comment(0)
C
0

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

It works for me.

Chisel answered 13/12, 2016 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.