2

I simply need an efficient way to debug GAE application, and to do so I need to connect to the production GAE infrastructure from the localhost when running dev_appserver.py.

Next code work well if I run it as a separate script:

import argparse

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

from google.appengine.ext import ndb
from google.appengine.ext.remote_api import remote_api_stub


def main(project_id):
    server_name = '{}.appspot.com'.format(project_id)
    remote_api_stub.ConfigureRemoteApiForOAuth(
        app_id='s~' + project_id,
        path='/_ah/remote_api',
        servername=server_name)

    # List the first 10 keys in the datastore.
    keys = ndb.Query().fetch(10, keys_only=True)

    for key in keys:
        print(key)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('project_id', help='Your Project ID.')

    args = parser.parse_args()

    main(args.project_id)

With this script, I was able to get data from remote Datastore. But where is I need to put the same code in my application(which is obviously not a single script) to make it work? I've tried to put remote_api_stub.ConfigureRemoteApiForOAuth() code in the appengine_config.py but I've got a recursion error. I'm running app like this:

dev_appserver.py app.yaml --admin_port=8001 --enable_console --support_datastore_emulator=no --log_level=info 

The application uses NDB to access Google Datastore. Application contain many modules and files and I simply don't know where is to put remote_api_stab auth code.

I hope somebody from the google team will see this topic because I've searched all the internet without any results. That's unbelievable how many people developing apps for the GAE platform, but it looks like nobody is developing/debugging apps locally.

Lem
  • 151
  • 6
  • 16
  • Just a suggestion but if you're just starting to build your application you might want to consider the Python 3 runtime for GAE instead of Python 2. The version of NDB that you're using is for Python 2 and that version will no longer be maintained after 2019. Google Cloud Datastore has two additional client libraries you might be interested in (1) https://pypi.org/project/google-cloud-datastore/ and (2) https://googleapis.dev/python/python-ndb/latest/index.html. The latter is still in Beta and not yet backward compatible with NDB from Python 2. – afed Oct 27 '19 at 15:37
  • Thank you @afed , is there is a chance to make dev_appserver communicate with datastore? – Lem Oct 27 '19 at 19:56
  • Short answer is yes. If you use the first client library (1) that I included in my previous comment it will use the production datastore by default. Just make sure the API is enabled in your project. Just keep in mind that this can cost you $. In the not too distant future you'll have to be more deliberate about storing your data locally when using Datastore by using the Datastore emulator which replaces the current implementation - https://cloud.google.com/datastore/docs/tools/datastore-emulator. – afed Oct 28 '19 at 03:05
  • As afed said if you want to debug locally it is recommended to use Datastore Emulator to avoid unexpected expenses. If still you want to connect with your localhost [here](https://cloud.google.com/datastore/docs/activate#other-platforms) it explains how to connect. As another option, if you want to debug the actual behavior of the interaction between App Engine and Datastore then I would recommend you to debug with Stackdriver to do this you can go to versions screen in App Engine and next to the version on diagnos you can click next to tools and select debug – Soni Sol Oct 30 '19 at 23:18

0 Answers0