简体   繁体   中英

Using a later version of six than 1.4.1 when running a python script

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'

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 (eg, z/usr/local/bin/python , or python3 , etc).
There is no need for
, etc).
There is no need for
, 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/xy/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. xy 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM