"ImportError: file_cache is unavailable" when using Python client for Google service account file_cache

Clay picture Clay · Oct 20, 2016 · Viewed 19.2k times · Source

I'm using a service account for G Suite with full domain delegation. I have a script with readonly access to Google Calendar. The script works just fine, but throws an error (on a background thread?) when I "build" the service. Here's the code:

from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
import urllib
import requests
from apiclient.discovery import build

cal_id = "[email protected]"

scopes                = ['https://www.googleapis.com/auth/calendar.readonly']
credentials           = ServiceAccountCredentials.from_json_keyfile_name('my_cal_key.json', scopes=scopes)
delegated_credentials = credentials.create_delegated('[email protected]')
http_auth             = delegated_credentials.authorize(Http())

# This is the line that throws the error
cal_service  = build('calendar','v3',http=http_auth)

#Then everything continues to work normally
request = cal_service.events().list(calendarId=cal_id)
response = request.execute()

# etc...

The error thrown is:

WARNING:googleapiclient.discovery_cache:file_cache is unavailable when using oauth2client >= 4.0.0
Traceback (most recent call last):
  File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
    from google.appengine.api import memcache
ImportError: No module named 'google'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module>
    from oauth2client.contrib.locked_file import LockedFile
ImportError: No module named 'oauth2client.contrib.locked_file'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/file_cache.py", line 37, in <module>
    from oauth2client.locked_file import LockedFile
ImportError: No module named 'oauth2client.locked_file'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/__init__.py", line 41, in autodetect
    from . import file_cache
  File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/file_cache.py", line 41, in <module>
    'file_cache is unavailable when using oauth2client >= 4.0.0')
ImportError: file_cache is unavailable when using oauth2client >= 4.0.0

What's going on here and is this something that I can fix? I've tried reinstalling and/or upgrading the google package.

Answer

Tomos Williams picture Tomos Williams · Jun 13, 2017

I am a bit late to the party here but I had a similar problem today and found the answer here

Solution to only the error : file_cache is unavailable when using oauth2client >= 4.0.0

Solution:

change your discovery.build() to have the field cache_discovery=False i.e

discovery.build(api, version, http=http, cache_discovery=False)

EDIT:

As @Chronial says this will disable the cache.

A solution that does not disable the cache can be found here