我和其他人在各种不同的GCP项目中,以及在几天前同一代码起作用的情况下,都遇到了以下问题。
我现在又走了一遍所有的台阶,只是为了确保我还能看到同样的东西。我从删除GCP项目中正在运行的Cloud实例开始,然后重新开始:首先部署Datalab,然后“开始使用”Datalab。
然后,我选择了一个预先存在的笔记本,它以以下几行开头:
!pip install --upgrade google-api-python-client
from httplib2 import Http
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
http = Http()
credentials.authorize(http)
这在过去对我和其他人都是有效的。现在我得到了这个ImportError:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-deffc94100d1> in <module>()
1 from httplib2 import Http
2 from oauth2client.client import GoogleCredentials
----> 3 credentials = GoogleCredentials.get_application_default()
4 http = Http()
5 credentials.authorize(http)
/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in get_application_default()
1202 GoogleCredentials.get_application_default().
1203
-> 1204 Args:
1205 access_token: string, access token.
1206 client_id: string, client identifier.
/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in _get_implicit_credentials(cls)
1187 """
1188
-> 1189 NON_SERIALIZED_MEMBERS = (
1190 frozenset(['_private_key']) |
1191 OAuth2Credentials.NON_SERIALIZED_MEMBERS)
/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in _implicit_credentials_from_gce()
1123
1124 def _in_gae_environment():
-> 1125 """Detects if the code is running in the App Engine environment.
1126
1127 Returns:
/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in _get_application_default_credential_GCE()
1378 """Get the Application Default Credentials for the current environment.
1379
-> 1380 Raises:
1381 ApplicationDefaultCredentialsError: raised when the credentials
1382 fail to be retrieved.
ImportError: No module named gce
有什么改变吗?以前被命名为"gce“的模块是否被重命名了?我需要创建凭据吗?(该项目有一个默认的APIs服务帐户和一个默认的Compute服务帐户,并且拥有我认为可能需要启用的所有API。)
2016年4月20日的最新消息:在一个月内没有重复这一消息后,我昨天又开始关注这个问题了。当我第一次试着重新运行上个月我正在使用的同一本笔记本时,为了让它正常工作,我需要拿出由于这个问题而添加的修复程序。但今天,同样的代码将再次失效。同时,我注意到建议已经更新了,所以现在我将介绍推荐的方法。
!pip show google-api-python-client
---
Name: google-api-python-client
Version: 1.5.0
Location: /usr/local/lib/python2.7/dist-packages
Requires: httplib2, uritemplate, six, oauth2client
从这里的角度看,所需的特定版本似乎是: httplib2>=0.8,<1;任意oauth2client;six>=1.6.1,<2;uritemplate>=0.6,<1。(允许早期oauth2client版本的更改似乎是在3月14日提交的,但Pypi显示最新的版本是2月20日上传的1.5.0,这与2月19日发布的发布页面表示一致,后者可能是这个问题最初出现的时候,尽管我几个星期没有注意到。)
在尝试使用Datalab时,我无法进一步尝试Anthonios描述的3种选项,因为在尝试使用Datalab时,我反复得到“连接失败”或其他错误。我很快就会再次更新这个问题。
发布于 2016-03-21 17:39:00
您可以在Datalab中安装其他python库,但必须确保安装不会破坏运行的Datalab环境。确保这一点的最佳方法是检查其他python库所需的依赖项当前是否安装在Datalab中。按照以下步骤执行:
步骤1:确认要安装的库需要哪些依赖项
使用!pip show <python library>
例如,使用以下方法查看google-api-python-client
所需的依赖关系:
>>> !pip show google-api-python-client
---
Name: google-api-python-client
Version: 1.5.0
Location: /usr/local/lib/python2.7/dist-packages
Requires: httplib2, uritemplate, six, oauth2client
所需的特定版本可以找到这里。
步骤2:确定任何依赖项是否已安装在Datalab中
在Datalab中,运行以下代码并检查步骤1中的依赖项
>>> import pip
>>> for dist in pip.get_installed_distributions():
>>> print dist
GCPData 0.1.0
GCPDataLab 0.1.0
wheel 0.26.0
tensorflow 0.6.0
protobuf 3.0.0a3
nbformat 4.0.1
ipykernel 4.2.2
jsonschema 2.5.1
nose 1.3.7
singledispatch 3.4.0.3
pyparsing 2.0.7
pandas 0.17.1
futures 3.0.3
pyasn1-modules 0.0.8
mock 1.3.0
MarkupSafe 0.23
cycler 0.9.0
ipython 4.0.3
terminado 0.6
path.py 8.1.2
certifi 2015.11.20.1
Pygments 2.1
funcsigs 0.4
backports-abc 0.4
jupyter-client 4.1.1
pexpect 4.0.1
backports.ssl-match-hostname 3.5.0.1
statsmodels 0.6.1
seaborn 0.6.0
scikit-learn 0.16.1
rsa 3.3
jupyter-core 4.0.6
brewer2mpl 1.4.1
py-dateutil 2.2
patsy 0.4.1
ptyprocess 0.5
PyYAML 3.11
Jinja2 2.8
decorator 4.0.6
pandocfilters 1.2.4
pickleshare 0.6
sympy 0.7.6
pytz 2015.7
httplib2 0.9.2
functools32 3.2.3-2
notebook 4.0.2
tornado 4.3
simplegeneric 0.8.1
numpy 1.10.4
matplotlib 1.5.1
scipy 0.17.0
pyasn1 0.1.9
pbr 1.8.1
python-dateutil 2.4.2
traitlets 4.1.0
oauth2client 1.4.12
ipython-genutils 0.1.0
nbconvert 4.1.0
ggplot 0.6.5
mistune 0.7.1
根据本例中的输出,一个冲突是Datalab当前安装了oauth2client
版本1.4.12
,而google-api-python-client
库需要大于或等于2.0.0
的oauth2client
版本。
我们有三个选择:
oauth2client
的支持升级到>=2.0.0
google-api-python-client
1.4.12安装的以前版本的oauth2client
。例如,google python-client 1.4.2google-api-python-client
,同时维护现有的oauth2client
安装。这可以通过在没有依赖项的情况下安装google-api-python-client
,然后手动安装任何在Datalab中尚未安装的缺失依赖项来实现。(库可能不能正常工作,但我们不太可能破坏工作的Datalab环境)。例如:
!pip install google-api-python-client==1.5.0 --no-deps
!pip install uritemplate
https://stackoverflow.com/questions/36142739
复制