0

AttributeError:“模块”对象没有属性“GoogleCredentials”

我有一个在 localhost 上运行的 appengine 应用程序。我有一些我运行的测试,我想使用 remote_api 来检查 db 值。当我尝试通过访问来访问 remote_api 时:

'http://127.0.0.1:8080/_ah/remote_api' 

我得到一个:

"This request did not contain a necessary header" 

但它在浏览器中工作。

当我现在尝试通过调用从我的测试中调用 remote_api

remote_api_stub.ConfigureRemoteApiForOAuth('localhost:35887','/_ah/remote_api')

我得到错误:

Error
Traceback (most recent call last):
  File "/home/dan/src/gtup/test/test_users.py", line 38, in test_crud
    remote_api_stub.ConfigureRemoteApiForOAuth('localhost:35887','/_ah/remote_api')
  File "/home/dan/Programs/google-cloud-sdk/platform/google_appengine/google/appengine/ext/remote_api/remote_api_stub.py", line 747, in ConfigureRemoteApiForOAuth
    credentials = client.GoogleCredentials.get_application_default()
AttributeError: 'module' object has no attribute 'GoogleCredentials'

我确实尝试重新安装整个谷歌云,但这没有用。

当我打开client.py

google-cloud-sdk/platform/google_appengine/lib/google-api-python-client/oauth2client/client.py 

我可以看到,remote_api_stub.py 使用它,其中没有 GoogleCredentials 类。

GoogleCredentials 类存在,但位于其他 client.py 文件中:

google-cloud-sdk/platform/google_appengine/lib/oauth2client/oauth2client/client.py
google-cloud-sdk/platform/gsutil/third_party/oauth2client/oauth2client/client.py
google-cloud-sdk/platform/bq/third_party/oauth2client/client.py
google-cloud-sdk/lib/third_party/oauth2client/client.py

我的 app.yaml 看起来像这样:

application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: true

libraries:
- name: webapp2
  version: latest

builtins:
- remote_api: on

handlers:
- url: /.*
  script: main.app

这只是 appengine 内部的错误导入/错误吗?还是我在单元测试中使用 remote_api 做错了什么?

4

2 回答 2

0

我通过替换文件夹解决了这个问题:

../google-cloud-sdk/platform/google_appengine/lib/google-api-python-client/oauth2client

和:

../google-cloud-sdk/platform/google_appengine/lib/oauth2client/oauth2client

包含在 google-api-python-client 文件夹中的那个现在在客户端文件中有所需的 Class: GoogleCredentials。

然后我遇到了第二个连接问题,现在我必须打电话:

remote_api_stub.ConfigureRemoteApiForOAuth('localhost:51805','/_ah/remote_api', False)

请注意,端口每次都会更改,服务器会重新启动。

于 2016-03-12T22:11:29.177 回答
0

回答而不是评论,因为我无法以我的声誉发表评论 -

在 mac 上运行这些类型的脚本时,我也发生了类似的事情。有时,您的 PATH 变量会混淆实际检查哪些文件的功能,尤其是当您在应用引擎启动器旁边安装了 gcloud 时。如果在 mac 上,我建议编辑/打开你的 ~/.bash_profile 文件来解决这个问题(或者可能是 ~/.bashrc,如果在 linux 上)。例如,在我的 Mac 上,我有以下几行来修复我的 PATH 变量:

export PATH="/usr/local/bin:$PATH"
export PYTHONPATH="/usr/local/google_appengine:$PYTHONPATH

这些基本上确保 python / 命令行将在 PATH(或 PYTHONPATH)中的任何内容之前查找 /usr/local/bin (或 /usr/local/google_appengine 在 PYTHONPATH 行的情况下)。

PATH 变量是当您在提示符中键入 python 文件时命令行检查它们的地方。PYTHONPATH 是您的 python 文件在运行时找到要加载的模块的地方。

于 2016-02-22T19:34:09.983 回答