【发布时间】:2018-12-27 14:08:13
【问题描述】:
我正在使用 App Engine Standard,开发 Python 后端服务,并且在某个时候,我告诉自己:
“嘿,你为什么不在使用远程数据存储时尝试在本地运行服务器”
我可以在本地运行这段代码,但我不知道为什么 remote_api_stub 会抛出错误:
" 文件 "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\remote_api\remote_api_stub.py", 第 1003 行,在 ConfigureRemoteApiForOAuth 'oauth2client 模块:%s' % e)
ImportError:使用 OAuth 凭据需要 oauth2client 模块:没有名为 oauth2client 的模块”
尽管根据 pip 的说法,我已经安装了 oauth2client(我在运行 pip install --upgrade oauth2client 时看到了这一点
提前感谢您的帮助
编辑:
我的问题不是 ImportError: No module named oauth2client 的重复。我还没有在 lib 文件夹中安装 google api 客户端,并确保“appengine_config.py”文件与必要的行位于其位置。
它仍然没有解决我的问题,所以我将提供更多详细信息!
这是我的 main.py(负责 GAE 的默认服务)或至少是它的脱脂版本:
import os
import datetime
import dev_appserver
from google.appengine.ext.remote_api import remote_api_stub
from google.appengine.api import apiproxy_stub_map
dev_appserver.fix_sys_path()
local_urlfetch_stub = apiproxy_stub_map.apiproxy.GetStub('urlfetch')
def configure_remote_api_dev_safe (host):
if not os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
logging.info("running on local server")
remote_api_stub.ConfigureRemoteApiForOAuth(host, '/_ah/remote_api')
apiproxy_stub_map.apiproxy.ReplaceStub('urlfetch', local_urlfetch_stub)
else:
logging.info("running on remote server")
def configure_remote_api_dev_unsafe (host):
if not os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
remote_api_stub.ConfigureRemoteApiForOAuth(host, '/_ah/remote_api')
else:
logging.info("running on remote server")
class Main(webapp2.RequestHandler):
def post(self):
configure_remote_api_dev_safe ('{}.appspot.com'.format(app))
test = Test(createdAt=datetime.datetime.utcnow())
test.put()
self.response.write("done")
app = ndb.toplevel(webapp2.WSGIApplication({('/', Main)}, debug=True))
当我请求默认服务时出现此错误:
PATH_TO_PROJECT_FOLDER>dev_appserver.py PATH_TO_PROJECT_FOLDER
INFO 2018-07-20 10:45:17,740 devappserver2.py:178] Skipping SDK update check.
INFO 2018-07-20 10:45:18,117 api_server.py:274] Starting API server at: http://localhost:53725
INFO 2018-07-20 10:45:18,151 dispatcher.py:270] Starting module "default" running at: http://localhost:8080
INFO 2018-07-20 10:45:18,153 admin_server.py:152] Starting admin server at: http://localhost:8000
INFO 2018-07-20 10:46:57,006 instance.py:294] Instance PID: 5940
WARNING 2018-07-20 08:46:57,565 sandbox.py:1086] The module _winreg is whitelisted for local dev only. If your application relies on _winreg, it is likely that it will not function properly in production.
INFO 2018-07-20 08:46:57,757 main.py:35] running on local server
ERROR 2018-07-20 08:46:57,760 webapp2.py:1528] Use of OAuth credentials requires the oauth2client module: No module named oauth2client
Traceback (most recent call last):
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in __call__
return handler.dispatch()
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "PATH_TO_PROJECT_FOLDER\main.py", line 125, in post
configure_remote_api_dev_safe ('{}.appspot.com'.format(app))
File "PATH_TO_PROJECT_FOLDER\main.py", line 38, in configure_remote_api_dev_safe'/_ah/remote_api')
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\remote_api\remote_api_stub.py", line 1003, in ConfigureRemoteApiForOAuth 'oauth2client module: %s' % e)
ImportError: Use of OAuth credentials requires the oauth2client module: No module named oauth2client
ERROR 2018-07-20 08:46:57,765 wsgi.py:279]
Traceback (most recent call last):
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\tasklets.py", line 1108, in add_context_wrapper
return synctaskletfunc(*args, **kwds)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\tasklets.py", line 1087, in synctasklet_wrapper
return taskletfunc(*args, **kwds).get_result()
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\ndb\tasklets.py", line 1057, in tasklet_wrapper
result = func(*args, **kwds)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 1077, in __call__
return handler.dispatch()
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\lib\webapp2-2.3\webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "PATH_TO_PROJECT_FOLDER\main.py", line 125, in post
configure_remote_api_dev_safe ('{}.appspot.com'.format(app))
File "PATH_TO_PROJECT_FOLDER\main.py", line 38, in configure_remote_api_dev_safe '/_ah/remote_api')
File "SDK_PATH\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\ext\remote_api\remote_api_stub.py", line 1003, in ConfigureRemoteApiForOAuth 'oauth2client module: %s' % e)
ImportError: Use of OAuth credentials requires the oauth2client module: No module named oauth2client
INFO 2018-07-20 10:46:57,782 module.py:880] default: "POST / HTTP/1.1" 500 -
我希望这更能说明问题。再次提前感谢您的帮助!
解决方案:(感谢@Dan Cornilescu)
=>永远不要忘记“import dev_appserver”和对“dev_appserver.fix_sys_path()”的调用
由于代码版本控制(一个糟糕的樱桃选择),我一开始就让它们丢失了,而我认为我做的一切都是正确的。
但我还没有完成,我正面临一个新问题,我将在一个新问题中输入一个新问题,因为它与这个问题的原因无关(我希望在某个时候不会丢失任何其他内容)。 [如果您对新一期感兴趣,请联系right here]
【问题讨论】:
-
如果重要,请不要这样做,但 doc 示例还显示了您没有的
dev_appserver.fix_sys_path()。 -
那是因为我在复制粘贴问题时错过了它,感谢您指出这一点。我相应地进行了编辑。
-
不,不是:/
-
我很抱歉@DanCornilescu 和snakecharmerb 我完全想念你的答案。我根据解决方案更新了我的问题(一段代码非常愚蠢的丢失)
标签: python-2.7 google-app-engine oauth2client