Search code examples
python-2.7google-app-engineremoteapi

"working outside of application context" in Google App Engine with Python remote API


I deployed one simple project on Google App Engine and there the project is working. Before I was able to get list of users from datastore through the remote API console , but now this no longer works. To start the remote API console I'm connecting this way (running from the project directory):

../../+/google_appengine_PYTHON_SDK/remote_api_shell.py -s project.appspot.com

I found that the easiest way to load all modules is to run:

s~project> help()
help>modules
help>quit

After that I try to get the list of users:

s~project> from app.models import User
s~project> User.query().fetch()

The last row results in this:

WARNING:root:suspended generator _run_to_list(query.py:964) raised RuntimeError(working outside of application context)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/utils.py", line 142, in positional_wrapper
    return wrapped(*args, **kwds)
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/query.py", line 1187, in fetch
    return self.fetch_async(limit, **q_options).get_result()
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/tasklets.py", line 325, in get_result
    self.check_success()
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/tasklets.py", line 368, in _help_tasklet_along
    value = gen.throw(exc.__class__, exc, tb)
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/query.py", line 964, in _run_to_list
    batch = yield rpc
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/tasklets.py", line 454, in _on_rpc_completion
    result = rpc.get_result()
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/api/apiproxy_stub_map.py", line 613, in get_result
    return self.__get_result_hook(self)
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/datastore/datastore_query.py", line 3014, in __query_result_hook
    self.__results = self._process_results(query_result.result_list())
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/datastore/datastore_query.py", line 3047, in _process_results
    for result in results]
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/datastore/datastore_rpc.py", line 185, in pb_to_query_result
    return self.pb_to_entity(pb)
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/model.py", line 662, in pb_to_entity
    entity = modelclass._from_pb(pb, key=key, set_key=False)
  File "/home/krasen/Programs/+/google_appengine_PYTHON_SDK/google/appengine/ext/ndb/model.py", line 3119, in _from_pb
    ent = cls()
  File "app/models.py", line 68, in __init__
    if self.email == current_app.config['MAIL_ADMIN']:
  File "/home/krasen/Programs/workspacePycharm/0010_1user_profile/venv/local/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/home/krasen/Programs/workspacePycharm/0010_1user_profile/venv/local/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
    return self.__local()
  File "/home/krasen/Programs/workspacePycharm/0010_1user_profile/venv/local/lib/python2.7/site-packages/flask/globals.py", line 34, in _find_app
    raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

I'm very new to python. I found http://kronosapiens.github.io/blog/2014/08/14/understanding-contexts-in-flask.html but couldn't understand from this how to start working inside the app context.

I have tried on linux with python versions:

  • 2.7.6(on clean installed linux)
  • 2.7.9

I have tried with google app engine SDK versions:

  • 1.9.17
  • 1.9.23
  • 1.9.24

Solution

  • The problem was in the User class itself. There I have:

    def __init__(self, **kwargs):
            super(User, self).__init__(**kwargs)
            #  FLASKY_ADMIN configuration variable, so as soon as that email address appears in a registration request it can be given the correct role
            if self.role is None:
                print ("there is no role for this user")
                if self.email == current_app.config['MAIL_ADMIN']:
                    self.role = ndb.Key('Role', 'administrator')
    

    the problematic line is this line:

    if self.email == current_app.config['MAIL_ADMIN']:
    

    I don't know how to manage to make it work with this line so I've removed it and now the user is retrieved.