1

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
makkasi
  • 6,328
  • 4
  • 45
  • 60
  • Which version of the SDK are you using? Can you run make sure to use the [latest version of the SDK](https://cloud.google.com/appengine/downloads#Google_App_Engine_SDK_for_Python)? – Nick Jul 27 '15 at 18:31
  • 1.9.17, 1.9.23, 1.9.24 (latest) . All do not work. I think that is not from the python SDK because one months ago it was working with 1.9.17 – makkasi Jul 28 '15 at 06:09
  • May be is not related but I've upgraded my linux kubuntu to plasma 5.2 and with that probably there are new packages for python. – makkasi Jul 28 '15 at 06:15
  • What version of python are you running on your system? That is, what's the output of `python -V`? – Nick Jul 28 '15 at 13:38
  • krasen@krasen-Lenovo-Y50-70:~$ python -V Python 2.7.9 – makkasi Jul 29 '15 at 07:02

1 Answers1

0

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.

makkasi
  • 6,328
  • 4
  • 45
  • 60
  • 2
    The way you solved your problem created a new one in that your source code now has admin credentials in it. For the solution, please see details on `with app.app_context()` here: http://stackoverflow.com/a/31444175 – matt Feb 26 '17 at 19:38
  • OK this is old one and what I have to say now is that the real problem is how Flask does access some things and how without flask context these things cannot be accessed. I forgot about this problem and the link above is the right one to look at. Thank you – makkasi Feb 27 '17 at 08:37