Windows 本地应用引擎使用:oauth2client ImportError
Windows local appengine usage: oauth2client ImportError
我正在使用 App Engine Standard,开发 Python 后端服务,在某些时候,我告诉自己:
"Hey why don't you try out and run the server locally while using the remote Datastore"
我可以在本地 运行 此代码,但我无法弄清楚为什么 remote_api_stub 会抛出错误:
" File "C:\Program Files (x86)\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"
即使根据 pip,我已经安装了 oauth2client(我看到当我 运行 pip install --upgrade 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]
正在将相关评论转换为答案。
您显示的代码片段似乎缺少 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
我正在使用 App Engine Standard,开发 Python 后端服务,在某些时候,我告诉自己:
"Hey why don't you try out and run the server locally while using the remote Datastore"
我可以在本地 运行 此代码,但我无法弄清楚为什么 remote_api_stub 会抛出错误:
" File "C:\Program Files (x86)\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"
即使根据 pip,我已经安装了 oauth2client(我看到当我 运行 pip install --upgrade oauth2client
提前感谢您的帮助
编辑:
我的问题不是
它仍然没有解决我的问题,所以我将提供更多详细信息!
这是我的 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]
正在将相关评论转换为答案。
您显示的代码片段似乎缺少 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