2

I am trying to achieve a simple goal: run python (2.7) script to read/update google sheet using Sheets API v4. I followed these steps:

  1. Created google cloud platform account
  2. Created service account and downloaded .json file with credentials
  3. Installed google client library
  4. Shared the spreadsheet with service account
  5. Wrote a very primitive script that should read the data from a spreadsheet

I have also read a lot of official documentation on how to connect python script to google sheets. Main articles and guides:

This is the script I am trying to run:

from googleapiclient.discovery import build
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SERVICE_ACCOUNT_FILE = 'service_account.json'
spreadsheet_id = '1KDCwwoIavfSINhDP9OKmieQux6LxhVC_ESsDQEma_7Y'
range_name = 'A1:A2'

creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('sheets', 'v4', credentials=creds)
result = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range_name).execute()
rows = result.get('values', [])
print('{0} rows retrieved.'.format(len(rows)))

When I run this script api call is not being made. Script always fails when build function tries to create service object that is needed to make a request. And this is the error log I am getting:

Traceback (most recent call last):
  File "google_sheets_update.py", line 15, in <module>
    service = build('sheets', 'v4', credentials=creds)
  File "/env/lib/python2.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/env/lib/python2.7/site-packages/googleapiclient/discovery.py", line 286, in build
    adc_key_path=adc_key_path,
  File "/env/lib/python2.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/env/lib/python2.7/site-packages/googleapiclient/discovery.py", line 455, in build_from_document
    if isinstance(client_options, six.moves.collections_abc.Mapping):
AttributeError: '_MovedItems' object has no attribute 'collections_abc'

I know that attribute error is explicit but I wasn't able to find any resource that will explain why this error occurs here and how to fix it. If someone could point me to what am I doing wrong and how to solve it I will appreciate it.

yury_hr
  • 33
  • 5
  • 1
    Turns out the solution was close and simple. My virtual env used google-api-python-client v `1.12.0`. When I switched it to `1.12.1` the problem left away. This post was helpful with installation of specific version: https://stackoverflow.com/questions/5226311/installing-specific-package-versions-with-pip Now the script runs as expected. Hope this helps someone who may face the same issue – yury_hr Sep 15 '20 at 18:03

0 Answers0