Kreender
Kreender

Reputation: 294

How to simulate app engine environment for a python3 application locally

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.

Upvotes: 2

Views: 597

Answers (1)

Bipolar Transistor
Bipolar Transistor

Reputation: 118

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)

Upvotes: 5

Related Questions