Windows local appengine usage: oauth2client ImportError

痞子三分冷 提交于 2020-01-13 19:14:13

问题


I m working with App Engine Standard, developing a Python backend service and, at some point, I told myself:

"Hey why don't you try out and run the server locally while using the remote Datastore"

I can run this code locally but I couldn't figure out why the remote_api_stub throws the error :

" 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"

Even though, according to pip, I got the oauth2client installed already (I see that when I run pip install --upgrade oauth2client

Thank you in advance for your help

EDIT:

My issue is not a duplicate of ImportError: No module named oauth2client. I yet did install google api client in the lib folder and made sure the "appengine_config.py" file was in its place with the necessary lines.

It still didn't resolve my issue, so I'm going to give more details about it !

Here is my main.py (taking care of the default service of GAE) or at least a degreased version of what it is:

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))

And I get this error when I request the default service:

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 -

I hope that this is more explanatory. Thank you again in advance for your help!

Solution : (Thanks to @Dan Cornilescu)

=> Simply never forget the "import dev_appserver" and the call to "dev_appserver.fix_sys_path()"

I had them lost at the first place because of code versioning (a bad cherry pick) while I thought I had everything right.

Yet I'm not done and I'm facing a new issue that I m going to type down in a new question because its unrelated to the cause of this one(I hope nothing else got lost at some point). [ if you are curious about the the new issue its right here]


回答1:


Converting the relevant comment to an answer.

The code snippet you shown seems to be missing this apparently needed setup section from the Using the Remote API in a local client example:

try:
    import dev_appserver
    dev_appserver.fix_sys_path()
except ImportError:
    print('Please make sure the App Engine SDK is in your PYTHONPATH.')
    raise


来源:https://stackoverflow.com/questions/51422605/windows-local-appengine-usage-oauth2client-importerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!