2
votes

I am using python google app engine

could you tell me, how i can run python3 google app engine with ndb on local system?

Help me

https://cloud.google.com/appengine/docs/standard/python3

3

3 Answers

2
votes

Please try this

Go to service account https://cloud.google.com/docs/authentication/getting-started

create json file

and add install this pip

$ pip install google-cloud-ndb

now open linux terminal

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials.json"

if window then open command prompt

set GOOGLE_APPLICATION_CREDENTIALS=C:\path\to\credentials.json

run this code in python3 in your terminal/command prompt

from google.cloud import ndb
client = ndb.Client()
with client.context():
    contact1 = Contact(name="John Smith",
                       phone="555 617 8993",
                       email="[email protected]")
    contact1.put()

see this result in your datastore.. Google console

1
votes

App Engine is a Serverless service provided by Google Cloud Platform where you can deploy your applications and configure Cloud resources like instances' CPU, memory, scaling method, etc. This will provide you the architecture to run your app.

This service is not meant to be used on local environments. Instead, it is a great option to host an application that (ideally) has been tested on local environments.

Let's say: You don't run a Django application with Datastore dependencies using App Engine locally, You run a Django application with Datastore (and other) dependencies locally and then deploy it to App Engine once it is ready.

Most GCP services have their Client libraries so we can interact with them via code, even on local environments. The ndb you asked belongs to the Google Cloud Datastore and can be installed in Python environments with:

pip install google-cloud-ndb

After installing it, you will be ready to interact with Datastore locally. Please find details about setting up credentials and code snippets in the Datastore Python Client Library reference.

Hope this is helpful! :)

1
votes

You can simply create emulator instance of the datastore on your local:

gcloud beta emulators datastore start --project test --host-port "0.0.0.0:8002" --no-store-on-disk --consistency=1

And then use it in the code in main app file:

from google.cloud import ndb

def get_ndb_client(namespace):

    if config.ENVIRONMENT != ENVIRONMENTS.LOCAL:
        # production
        db = ndb.Client(namespace=namespace)
    else:
        # localhost
        import mock

        credentials = mock.Mock(spec=google.auth.credentials.Credentials)
        db = ndb.Client(project="test", credentials=credentials, namespace=namespace)

    return db

ndb_client = get_ndb_client("ns1")