"ImportError: file_cache is unavailable" when using Python client for Google service account file_cache
"ImportError: file_cache is unavailable" when using Python client for Google service account file_cache
我使用的是 G Suite 服务帐户,具有完整的域授权。我有一个只读访问 Google 日历的脚本。该脚本工作正常,但在我 "build" 服务时抛出错误(在后台线程上?)。这是代码:
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
import urllib
import requests
from apiclient.discovery import build
cal_id = "my_calendar_id@group.calendar.google.com"
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('me@mydomain.com')
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...
抛出的错误是:
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
这是怎么回事,我可以解决这个问题吗?我尝试重新安装 and/or 升级 google
包。
要为 Python 客户端使用 Google API,您需要先安装它,因为 Google API 没有内置在Python 个模块。该说明可在 Install the Library.
中找到
安装
您可以使用包管理器或手动下载并安装 Python 客户端库:
托管安装
使用 pip 或 setuptools 管理您的安装(您可能需要先 运行 sudo):
pip(首选):
$ pip install --upgrade google-api-python-client
Setuptools:使用 setuptools 包中包含的 easy_install 工具:
$ easy_install --upgrade google-api-python-client
模块"google-api-python-client"的代码头说...
install_requires = [
'httplib2>=0.9.2,<1dev',
'oauth2client>=1.5.0,<5.0.0dev', <<=============
'six>=1.6.1,<2dev',
'uritemplate>=3.0.0,<4dev',
]
所以,我已经卸载了 oauth2client 版本 4.0.0
然后,我从官方 python 站点 https://pypi.python.org/pypi/oauth2client/1.5.2
下载了一个 tar.gz 文件中的 oauth2client 1.5.2
我已经安装了这个下载的文件,所以我有 1.5.2 版本的 oauth2client
Package Version
------------------------ ---------
certifi 2016.9.26
discovery 0.0.4
distribute 0.7.3
future 0.16.0
google-api-python-client 1.5.5
httplib2 0.9.2
oauth2client 1.5.2
pefile 2016.3.28
pip 9.0.1
pyasn1 0.1.9
pyasn1-modules 0.0.8
PyInstaller 3.2
pypiwin32 219
requests 2.11.1
rsa 3.4.2
setuptools 28.8.0
six 1.10.0
uritemplate 3.0.0
之后,ALL再次正常工作,没有警告消息。
我来这里有点晚了,但我今天遇到了类似的问题并找到了答案here
仅错误的解决方案:file_cache is unavailable when using oauth2client >= 4.0.0
解决方案:
将您的 discovery.build()
更改为具有字段 cache_discovery=False
即
discovery.build(api, version, http=http, cache_discovery=False)
我不想降级我的 oauth2client。当您为 apiclient.discovery.build
设置 cache_discovery=False
时,您可以使用 oauth2client 4.1.2。这个简单的解决方案消除了错误:
service = discovery.build(api_name,
api_version,
credentials=credentials,
cache_discovery=False)
顺便说一句,这是我的回答的转贴 here
由于它们在技术上是警告(不是错误),如果您无法直接访问调用,则可以在实例化服务的任何地方执行此操作以抑制它们:
import logging
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)
感谢 theacodes who gave that answer here(但 'client' 中缺少 'n',这似乎导致那里有很多反对票)。
另见讨论 here
以下是我如何使用 google-api-python-client
在 Cloud Functions 上构建服务。
# for Cloud Functions use
def get_service():
import googleapiclient.discovery
return googleapiclient.discovery.build('compute', 'v1', cache_discovery=False)
终于成功了。
尝试了列出的所有解决方案,但其中 none 有效。直到我尝试了@dtk 的(简单)建议,该建议列在评论的某处:
通过 运行 安装旧版本的 oauth2client:
pip install oauth2client==3.0.0
现在一切正常。谢谢@dtk!
添加到所选答案,如果 build
函数被依赖项调用,并且没有简单的方法来更新它的代码,则可以使用此代码强制 cache_discovery
成为 False
:
import googleapiclient.discovery
_build = googleapiclient.discovery.build
def no_cache_build(*args, **kw):
kw["cache_discovery"] = False
return _build(*args, **kw)
googleapiclient.discovery.build = no_cache_build
我使用的是 G Suite 服务帐户,具有完整的域授权。我有一个只读访问 Google 日历的脚本。该脚本工作正常,但在我 "build" 服务时抛出错误(在后台线程上?)。这是代码:
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
import urllib
import requests
from apiclient.discovery import build
cal_id = "my_calendar_id@group.calendar.google.com"
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('me@mydomain.com')
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...
抛出的错误是:
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
这是怎么回事,我可以解决这个问题吗?我尝试重新安装 and/or 升级 google
包。
要为 Python 客户端使用 Google API,您需要先安装它,因为 Google API 没有内置在Python 个模块。该说明可在 Install the Library.
中找到安装
您可以使用包管理器或手动下载并安装 Python 客户端库:
托管安装
使用 pip 或 setuptools 管理您的安装(您可能需要先 运行 sudo):
pip(首选):
$ pip install --upgrade google-api-python-client
Setuptools:使用 setuptools 包中包含的 easy_install 工具:
$ easy_install --upgrade google-api-python-client
模块"google-api-python-client"的代码头说...
install_requires = [
'httplib2>=0.9.2,<1dev',
'oauth2client>=1.5.0,<5.0.0dev', <<=============
'six>=1.6.1,<2dev',
'uritemplate>=3.0.0,<4dev',
]
所以,我已经卸载了 oauth2client 版本 4.0.0
然后,我从官方 python 站点 https://pypi.python.org/pypi/oauth2client/1.5.2
下载了一个 tar.gz 文件中的 oauth2client 1.5.2我已经安装了这个下载的文件,所以我有 1.5.2 版本的 oauth2client
Package Version
------------------------ ---------
certifi 2016.9.26
discovery 0.0.4
distribute 0.7.3
future 0.16.0
google-api-python-client 1.5.5
httplib2 0.9.2
oauth2client 1.5.2
pefile 2016.3.28
pip 9.0.1
pyasn1 0.1.9
pyasn1-modules 0.0.8
PyInstaller 3.2
pypiwin32 219
requests 2.11.1
rsa 3.4.2
setuptools 28.8.0
six 1.10.0
uritemplate 3.0.0
之后,ALL再次正常工作,没有警告消息。
我来这里有点晚了,但我今天遇到了类似的问题并找到了答案here
仅错误的解决方案:file_cache is unavailable when using oauth2client >= 4.0.0
解决方案:
将您的 discovery.build()
更改为具有字段 cache_discovery=False
即
discovery.build(api, version, http=http, cache_discovery=False)
我不想降级我的 oauth2client。当您为 apiclient.discovery.build
设置 cache_discovery=False
时,您可以使用 oauth2client 4.1.2。这个简单的解决方案消除了错误:
service = discovery.build(api_name,
api_version,
credentials=credentials,
cache_discovery=False)
顺便说一句,这是我的回答的转贴 here
由于它们在技术上是警告(不是错误),如果您无法直接访问调用,则可以在实例化服务的任何地方执行此操作以抑制它们:
import logging
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)
感谢 theacodes who gave that answer here(但 'client' 中缺少 'n',这似乎导致那里有很多反对票)。
另见讨论 here
以下是我如何使用 google-api-python-client
在 Cloud Functions 上构建服务。
# for Cloud Functions use
def get_service():
import googleapiclient.discovery
return googleapiclient.discovery.build('compute', 'v1', cache_discovery=False)
终于成功了。
尝试了列出的所有解决方案,但其中 none 有效。直到我尝试了@dtk 的(简单)建议,该建议列在评论的某处:
通过 运行 安装旧版本的 oauth2client:
pip install oauth2client==3.0.0
现在一切正常。谢谢@dtk!
添加到所选答案,如果 build
函数被依赖项调用,并且没有简单的方法来更新它的代码,则可以使用此代码强制 cache_discovery
成为 False
:
import googleapiclient.discovery
_build = googleapiclient.discovery.build
def no_cache_build(*args, **kw):
kw["cache_discovery"] = False
return _build(*args, **kw)
googleapiclient.discovery.build = no_cache_build