GAnalytics:脚本正在运行,现在找不到多个模块(GAnalytics 库)
GAnalytics : script was working, now multiple module not found (GAnalytics lib)
我对一个完美运行了 2 个月的脚本进行了一些修改。一些代码分解,在这里和那里添加一些日志记录……没什么大不了的。
现在,当我尝试我的脚本时,我遇到了一些错误,一些 google 模块(我从 Google 此处的 Analytics 获取数据)丢失了?
我尝试更新所有软件包(我使用的是 conda),但我一直遇到同样的错误:
WARNING:googleapiclient.discovery_cache:file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth
Traceback (most recent call last):
File "/Users/gil/anaconda3/envs/GA2DBenv/lib/python3.7/site-packages/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
from google.appengine.api import memcache
ModuleNotFoundError: No module named 'google.appengine'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/gil/anaconda3/envs/GA2DBenv/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module>
from oauth2client.contrib.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.contrib.locked_file'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/gil/anaconda3/envs/GA2DBenv/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 37, in <module>
from oauth2client.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.locked_file'
一般来说,问题在于 Google 打包和维护模块的方式。
在我的环境中有效的解决方案如下:
ModuleNotFoundError: No module named 'google.appengine'
:
https://github.com/GoogleCloudPlatform/getting-started-python/issues/157#issuecomment-577467711
ModuleNotFoundError: No module named
'oauth2client.contrib.locked_file'
和 ModuleNotFoundError: No
module named 'oauth2client.locked_file
':
https://github.com/googleapis/google-api-python-client/issues/325#issuecomment-274349841
- 请注意,文件缓存修复是 silencing/removing locked_file 警告,因为 default/built-in 缓存已禁用或替换为自定义 Class(https://github.com/googleapis/google-api-python-client/issues/325#issuecomment-419387788).为了您的方便,这里是实际的代码片段:
禁用文件缓存(错误的解决方案):
discovery.build('drive', 'v3', http=http, cache_discovery=False)
文件缓存解决方法(更好的解决方案):
import os.path
import hashlib
import tempfile
class DiscoveryCache:
def filename(self, url):
return os.path.join(
tempfile.gettempdir(),
'google_api_discovery_' + hashlib.md5(url.encode()).hexdigest())
def get(self, url):
try:
with open(self.filename(url), 'rb') as f:
return f.read().decode()
except FileNotFoundError:
return None
def set(self, url, content):
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(content.encode())
f.flush()
os.fsync(f)
os.rename(f.name, self.filename(url))
discovery.build('drive', 'v3', http=http, cache=DiscoveryCache())
希望对您有所帮助。
我对一个完美运行了 2 个月的脚本进行了一些修改。一些代码分解,在这里和那里添加一些日志记录……没什么大不了的。
现在,当我尝试我的脚本时,我遇到了一些错误,一些 google 模块(我从 Google 此处的 Analytics 获取数据)丢失了?
我尝试更新所有软件包(我使用的是 conda),但我一直遇到同样的错误:
WARNING:googleapiclient.discovery_cache:file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth
Traceback (most recent call last):
File "/Users/gil/anaconda3/envs/GA2DBenv/lib/python3.7/site-packages/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
from google.appengine.api import memcache
ModuleNotFoundError: No module named 'google.appengine'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/gil/anaconda3/envs/GA2DBenv/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module>
from oauth2client.contrib.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.contrib.locked_file'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/gil/anaconda3/envs/GA2DBenv/lib/python3.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 37, in <module>
from oauth2client.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.locked_file'
一般来说,问题在于 Google 打包和维护模块的方式。
在我的环境中有效的解决方案如下:
ModuleNotFoundError: No module named 'google.appengine'
: https://github.com/GoogleCloudPlatform/getting-started-python/issues/157#issuecomment-577467711ModuleNotFoundError: No module named 'oauth2client.contrib.locked_file'
和ModuleNotFoundError: No module named 'oauth2client.locked_file
': https://github.com/googleapis/google-api-python-client/issues/325#issuecomment-274349841- 请注意,文件缓存修复是 silencing/removing locked_file 警告,因为 default/built-in 缓存已禁用或替换为自定义 Class(https://github.com/googleapis/google-api-python-client/issues/325#issuecomment-419387788).为了您的方便,这里是实际的代码片段:
禁用文件缓存(错误的解决方案):
discovery.build('drive', 'v3', http=http, cache_discovery=False)
文件缓存解决方法(更好的解决方案):
import os.path
import hashlib
import tempfile
class DiscoveryCache:
def filename(self, url):
return os.path.join(
tempfile.gettempdir(),
'google_api_discovery_' + hashlib.md5(url.encode()).hexdigest())
def get(self, url):
try:
with open(self.filename(url), 'rb') as f:
return f.read().decode()
except FileNotFoundError:
return None
def set(self, url, content):
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(content.encode())
f.flush()
os.fsync(f)
os.rename(f.name, self.filename(url))
discovery.build('drive', 'v3', http=http, cache=DiscoveryCache())
希望对您有所帮助。