appengine python remote_api模块对象没有属性GoogleCredentials

时间:2016-02-14 14:47:32

标签: python-unittest google-app-engine-python remoteapi

AttributeError:' module'对象没有属性' GoogleCredentials'

我有一个在localhost上运行的appengine应用程序。 我有一些测试,我运行,我想使用remote_api检查数据库值。 当我尝试访问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做错了什么?

2 个答案:

答案 0 :(得分: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 /命令行将在/ usr / local / bin(或者在PYTHONPATH行的情况下为/ usr / local / google_appengine)中查找PATH(或PYTHONPATH)中的任何内容。

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

答案 1 :(得分: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文件夹中的文件现在在客户端文件中包含所需的类:GoogleCredentials。

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

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

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

相关问题