Q:

RuntimeError when running a simple flask_restful app on Google App Engine

I have the following very simple app:
from lib.flask import Flask

from lib.flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class TestResource(Resource):
    def get(self):
        return {"a":"b","c":"d"}
api.add_resource(TestResource, "/")

When I run this, I get the following Exception:
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 266, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/Users/me/dev/project/src/main.py", line 22, in do_in_request
    app(*args, **kwargs)
  File "/Users/me/dev/project/src/lib/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/me/dev/project/src/lib/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/me/dev/project/src/lib/flask_restful/__init__.py", line 249, in error_router
    if self._has_fr_route():
  File "/Users/me/dev/project/src/lib/flask_restful/__init__.py", line 230, in _has_fr_route
    if self._should_use_fr_error_handler():
  File "/Users/me/dev/project/src/lib/flask_restful/__init__.py", line 211, in _should_use_fr_error_handler
    adapter = self.app.create_url_adapter(request)
  File "/Users/me/dev/project/src/lib/flask/app.py", line 1601, in create_url_adapter
    return self.url_map.bind_to_environ(request.environ,
  File "/Users/me/dev/project/src/lib/werkzeug/local.py", line 338, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/Users/me/dev/project/src/lib/werkzeug/local.py", line 297, in _get_current_object
    return self.__local()
  File "/Users/me/dev/project/src/lib/flask/globals.py", line 20, in _lookup_req_object
    raise RuntimeError('working outside of request context')
RuntimeError: working outside of request context

So I tried to put my entire app into what I thought would be the request context. In the code above, both the Flask object and the Api object are being instantiated once, and can be called by the server multiple times. From the traceback, it looks like the instantiation should happen within a request context, so I wrapped it like this:
def do_in_request(*args, **kwargs):
    from lib.flask import Flask

    from lib.flask_restful import Resource, Api

    app = Flask(__name__)
    api = Api(app)

    class TestResource(Resource):
        def get(self):
            return {"a":"b","c":"d"}
    api.add_resource(TestResource, "/")

    app(*args, **kwargs)

app = do_in_request

That still raises the same error. What's going on, what do they mean by 'request context', and how do I fix this?

A:

I started from scratch using the App Engine Flask Skeleton and added flask-restful as a dependency in requirements.txt and then just added more or less the code you have above and it worked without an issue. I added the repository to my github here if you want to use it - you can see the changes I made to the skeleton in this commit.
I'm not sure why your example isn't working, but one thing I noticed that could be causing issues is that you're doing from lib.flask .... It seems your third-party packages live in lib and you haven't necessarily "lifted" lib into your sys.path. The skeleton includes a vendoring library that makes sure that this is handled properly. It might be a potential source of the problem.
Either way, I hope the forked skeleton can help you get up and running.

