2

I'm creating a micro service that should run on a python 3 standard environment of Google App Engine. I want to run it in a simulated Google app engine environment. In the python2 standard environment you could do something like:

dev_appserver.py [PATH_TO_YOUR_APP]

but that is not supported for python and I haven't found and equivalent in the documentation, and I need it to test my app locally with datastore.

Kreender
  • 284
  • 1
  • 6
  • 17
  • Possible duplicate of [Running locally with GAE Python second generation](https://stackoverflow.com/questions/54640695/running-locally-with-gae-python-second-generation) – new name Feb 13 '19 at 16:49

1 Answers1

5

dev_appserver.py is still available in the GAE Python 3 run-time, it's not recommended though, as it goes against the idiomatic sandbox they are now going for. To make it work, there are a few extra steps to what you're used to. First, run in your terminal cloud beta emulators datastore env-init to get your projects' datastore emulator environment variables. If everything is default, they should look something like this:

    DATASTORE_DATASET=your-project-name
    DATASTORE_EMULATOR_HOST=localhost:8081
    DATASTORE_EMULATOR_HOST_PATH=localhost:8081/datastore
    DATASTORE_HOST=http://localhost:8081
    DATASTORE_PROJECT_ID=your-project-id

Now apply this variables to the terminal your will run dev_appserver.py with the following (this will make the datastore viewer in dev_appserver.py connect properly to the datastore emulator):

$ $(gcloud beta emulators datastore env-init)

Now you need to pass this variables to the python venv inside dev_appserver.py with the following argument structure (this will make your app's google cloud libraries connect properly to the datastore emulator):

$ dev_appserver.py \
--application=your-project-name \
--env_var DATASTORE_DATASET=your-project-name \
--env_var DATASTORE_EMULATOR_HOST=localhost:8081 \
--env_var DATASTORE_EMULATOR_HOST_PATH=localhost:8081/datastore \
--env_var DATASTORE_HOST=http://localhost:8081 \
--env_var DATASTORE_EMULATOR_HOST_PATH=localhost:8081/datastore \
  [PATH_TO_YOUR_APP]

Finally, open a second terminal and run the datastore emulator with:

$ gcloud beta emulators datastore start

You should have everything running. Note that I assumed your project is correctly setup for the Python 3 environment (the new app.yaml structure and the presence of the requirements.txt file)

  • Haven't tried this yet but I had a question. If the dev_appserver way is not recommended, what would be the official way of using datastore locally? – Kreender Feb 12 '19 at 20:48
  • Now you should use something like virtualenv to isolate your app and use only google cloud libraries to access other google services, like datastore. If you're local, some of these services can be emulated. Keep in mind that all App Engine APIs libraries, such as ndb are unsupported on Python 3. Take a look at this article [here](https://cloud.google.com/appengine/docs/standard/python3/python-differences). I'm also writing a series of posts regarding my experience migrating to the Python 3 runtime, feel free to check it out [here](https://www.bernardorodrigues.org/blog) – Bipolar Transistor Feb 12 '19 at 23:21
  • At least in the short term, it is a lot easier to not use the datastore emulator with the `--support_datastore_emulator=False` flag to `dev_appserver.py` – new name Feb 13 '19 at 16:51
  • The best ever documentation I read till now for using datastore emulator... Thanks a lot... you save my day! – Jay Patel Oct 09 '19 at 06:25