1

Trying to use the google api client, I have gotten an error that MANY others have gotten:

AttributeError: 'Module_six_moves_urllib_parse' object has no attribute 'urlencode'

I have tried every solution found in StackOverflow, GitHub, and other places, including:

1) from this thread, changing the path in the actual code:

import sys
sys.path.insert(1, '/Library/Python/2.7/site-packages')

2) from this thread, changing the python path in the .bashrc and .bash_profile files:

pip show six | grep "Location:" | cut -d " " -f2
export PYTHONPATH=$PYTHONPATH:<pip_install_path>
source ~/.bashrc

3) and from this thread, downgrading my google api client to 1.3.2 (or at least trying to).

I'm new to programming so this could be a basic problem but I've spent days trying to troubleshoot and to no avail. It seems like no matter what I do, the old 1.4 version of six is being used. Any help you could provide would be MUCH appreciated!

EDIT: Full Traceback:

Traceback (most recent call last):
  File "/Users/zachgoldfine/PycharmProjects/FirstTry/GetAroundRentalSpreadsheetRead.py",     line 71, in <module>
    spreadsheetId=spreadsheetId, range=rangeName1).execute()
  File "/Library/Python/2.7/site-packages/oauth2client/util.py", line 129, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/googleapiclient/http.py", line 836, in execute
    method=str(self.method), body=self.body, headers=self.headers)
  File "/Library/Python/2.7/site-packages/googleapiclient/http.py", line 162, in _retry_request
    resp, content = http.request(uri, method, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/oauth2client/transport.py", line 186, in new_request
    credentials._refresh(orig_request_method)
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 761, in _refresh
    self._do_refresh_request(http)
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 774, in _do_refresh_request
    body = self._generate_refresh_request_body()
  File "/Library/Python/2.7/site-packages/oauth2client/client.py", line 716, in _generate_refresh_request_body
    body = urllib.parse.urlencode({
AttributeError: 'Module_six_moves_urllib_parse' object has no attribute     'urlencode'
Community
  • 1
  • 1
  • Could you include the full traceback, so we can see from which file(s) the error originates? –  Feb 28 '17 at 00:47
  • @Evert added the full traceback – Zach Goldfine Feb 28 '17 at 01:30
  • I see a mention of pycharm in the traceback. Are you running things through pycharm? That may be an issue. I'm not familiar with pycharm, but if you can avoid it and try to run the relevant script(s) from the command line, that could help narrowing down the problem cause. –  Feb 28 '17 at 03:15

1 Answers1

0

Assuming it's indeed a problem with the six version, here is one way to install six and be able to use the newly installed version.

Important note: this will only work from your user account; not from any other account.
On the safe side: this will not alter the system Python environment, that is, system scripts that may use Python will continue to use the older pip version.

Firstly, reverse the three steps above. In particular, manually altering sys.path in a script should really very rarely be necessary.

Then, use the --user option, which installs a local version, that Python (when run by that user) will automatically pick up first. To make sure the Python executable you are using corresponds to the pip module and (later) the installed six module, use the following:

python -m pip install six --user

where python may be something slightly else, if you happen to not use the system Python (e.g., z/usr/local/bin/python, orpython3, etc).
There is no need for
sudo` or similar.

If pip complains that the requirement is already up to date (it shouldn't, or you wouldn't have gotten the above problems), try:

python -m pip install six --user --upgrade --force

Once done, you can check $HOME/Library/Python/x.y/lib/python/site-packages to see if you see the correct version of six there. This is your local user Library directory, not the system one. x.y is probably 2.7, but do check that python is actually that version.


The problem may also be with the google api client. I don't know if that has a pip installation, but otherwise you could try something similar as for six:

python -m pip install <google-api-client> --user (--upgrade --force)
  • Thank you for your response. I followed your steps by first getting rid of the bashrc add-ons and then deleting the sys.path.insert bit. When I tried your first suggestion, I got "Requirement already satisfied..." so I tried your second suggestion with the added --upgrade --force, and that seemed to successfully install an upgraded six (version 1.10). Next, I checked the directory you suggested and I got `no such file or directory: /Users/zachgoldfine/Library/Python/2.7/lib/python/site-packages` – Zach Goldfine Feb 28 '17 at 01:39
  • From what I understand, it seems like google api client needs six 1.6 or above. But when I run the script, it seems to use the 1.4 version of six every time, causing the error. How might I point Python / the interpreter (sorry if my vocab is off here) to the updated version of six instead of the older one? – Zach Goldfine Feb 28 '17 at 01:42
  • @ZachGoldfine Two tests: 1/ can you see if the directory `$HOME/.local/lib/python2.7/site-packages` exists? 2/ can you just run Python itself and import six on the Python prompt, then check the version: `>>> import six; six.__version__` should do it. –  Feb 28 '17 at 03:13