Home > Net >  Python3 dev_appserver and datastore
Python3 dev_appserver and datastore

Time:09-21

I am creating a small Python 3 app for appengine, and I'd expect this to use the local datastore (which to the best of my understanding is a .db file).

I am starting the app using dev_appserver.py ., as per docs I have exposed a service account using GOOGLE_APPLICATION_CREDENTIALS env var (ages ago, when Python2 was the only supported runtime, this was not needed), and I still have a 403 permission error if I try to create any entity in the datastore.

Isn't this supposed to use local datastore ?

Should I use the datastore emulator ?

Is the use of dev_appserver.py still needed, or I can just start the app using a proper python ... command ?

Thanks, it's more than 10 years I am out of appengine, and it's just for a local test I am doing.

P.s.: I am using google.cloud.ndb to access the datastore.

CodePudding user response:

I have had this same experience. It seems gcloud CLI is trying to ensure that the project (your local project) exists in Google Cloud and the service account specified has access to it (this was a different behavior from Python 2).

A work around is - according to Google, you can run your Python App using the development tools that you usually use e.g. python xyz, flask run abc etc. If you do it this way, it's best to use datastore emulator. The downside with this approach is that you lose access to a GUI for viewing your data.

CodePudding user response:

I use a Makefile to start things needed for both running on localhost and for tests. Here is the most relevant snippet:

run: stop
  cd static/js-src; rollup -cw --no-watch.clearScreen &
  sass -qw static/sass:static/css &
  gcloud beta emulators datastore start --consistency=1.0 --host-port=:8081 &
  gcloud beta emulators datastore start --consistency=1.0 --host-port=:8082 --no-store-on-disk &
  redis-server --save "" &
  sleep 2
  python main.py

This is what it does in order:

  • Compiles javascript with rollup
  • Compiles sass
  • Starts one instance of datastore emulator on port 8081 for running on localhost. This datastore is stored to disk so my data remains after stopping and starting again.
  • Starts a second instance of datastore emulator on port 8082 for tests. This doesn't store on disk. This allows me to run on localhost and run tests simultaneously without them interfering with each other.
  • My app uses redis
  • Sleep to allow the datastore emulator to complete startup
  • Run app on localhost

I have a separate stop command to stop everything.

  • Related