我正在使用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 module:没有名为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感到好奇,则
答案 0 :(得分:2)
将相关评论转换为答案。
您显示的代码片段似乎缺少Using the Remote API in a local client示例中的此明显需要的设置部分:
try:
import dev_appserver
dev_appserver.fix_sys_path()
except ImportError:
print('Please make sure the App Engine SDK is in your PYTHONPATH.')
raise