Reputation: 33
I'm trying to run the Google App Engine local development server (dev_appserver.py
) together with the Cloud Datastore Emulator.
Environment:
/usr/bin/python2 --version
: 2.7.18./venv/bin/python2 --version
: 2.7.18/usr/bin/python3 --version
: 3.10.6/usr/bin/gcloud --version
:Google Cloud SDK 418.0.0
alpha 2023.02.13
app-engine-python 1.9.101
beta 2023.02.13
bq 2.0.85
bundled-python3-unix 3.9.16
cloud-datastore-emulator 2.3.0
core 2023.02.13
gcloud-crc32c 1.0.0
gsutil 5.20
/usr/bin/java --version
:openjdk 11.0.17 2022-10-18
OpenJDK Runtime Environment (build 11.0.17+8-post-Ubuntu-1ubuntu222.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-post-Ubuntu-1ubuntu222.04, mixed mode, sharing)
Background: I'm migrating an existing Python 2 server to Python 3.
Minimal scenario to reproduce the problem:
gae.sh
:
#! /usr/bin/env bash
export DATASTORE_DATASET=myproject
export DATASTORE_EMULATOR_HOST=localhost:8087
export DATASTORE_EMULATOR_HOST_PATH=localhost:8087/datastore
export DATASTORE_HOST=http://localhost:8087
export DATASTORE_PROJECT_ID=myproject
python2 \
/usr/bin/dev_appserver.py \
--runtime_python_path=/usr/bin/python3 \
--application=myproject \
--host=localhost \
--port=8082 \
--support_datastore_emulator=true \
--require_indexes=true \
--running_datastore_emulator_host=localhost:8087 \
--datastore_emulator_port=8087 \
--log_level=debug \
--dev_appserver_log_level=debug \
./app.yaml
datastore.sh
:
#! /usr/bin/env bash
gcloud beta emulators datastore start \
--project=myproject \
--host-port=localhost:8087 \
--verbosity=debug
app.yaml
:
# Same result with python35 python36 python37 python38 python39 python310 python311
runtime: python310
# Any other options are not changing the outcome
# app_engine_apis: true
# service: default
# entrypoint: python3 -m main
requirements.txt
:
# The emulator never reaches the point to install dependencies
# appengine-python-standard>=1.0.0
# Flask==1.1.4
# MarkupSafe==2.0.1
main.py
:
# The WSGI application is not run at all (regardless of the app.yaml options)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello, World!'
if __name__ == "__main__":
app.run(host='localhost', port=8083, debug=True)
Set up and activate the virtual environment:
virtualenv -p /usr/bin/python2 ./venv
source ./venv/bin/activate
grpcio
is required by the emulator (https://cloud.google.com/appengine/docs/legacy/standard/python/tools/migrate-cloud-datastore-emulator):
pip2 install grpcio
To attempt starting the emulator, simply run:
./gae.sh
Which outputs:
WARNING: Python 2 will soon be deprecated by the Google Cloud SDK
and may not function correctly. Please use Python version 3.5 and up.
If you have a compatible Python interpreter installed, you can use it by setting
the CLOUDSDK_PYTHON environment variable to point to it.
WARNING: Python 2 will soon be deprecated by the Google Cloud SDK
and may not function correctly. Please use Python version 3.5 and up.
If you have a compatible Python interpreter installed, you can use it by setting
the CLOUDSDK_PYTHON environment variable to point to it.
INFO 2023-02-24 11:20:19,042 devappserver2.py:240] Using Cloud Datastore Emulator.
We are gradually rolling out the emulator as the default datastore implementation of dev_appserver.
If broken, you can temporarily disable it by --support_datastore_emulator=False
Read the documentation: https://cloud.google.com/appengine/docs/standard/python/tools/migrate-cloud-datastore-emulator
Help us validate that the feature is ready by taking this survey: <**edited out because not allowed on Stack Overflow**>
Report issues at: https://issuetracker.google.com/issues/new?component=187272
INFO 2023-02-24 11:20:19,056 devappserver2.py:317] Skipping SDK update check.
WARNING 2023-02-24 11:20:19,428 <string>:711] Detected environment variable DATASTORE_EMULATOR_HOST=localhost:8087, dev_appserver will speak to the Cloud Datastore emulator running on this address. The datastore_path /tmp/appengine.myproject.myname/datastore.db will be neglected.
If you want datastore to store on /tmp/appengine.myproject.myname/datastore.db, remove DATASTORE_EMULATOR_HOST from environment variables and restart dev_appserver
INFO 2023-02-24 11:20:19,610 <string>:384] Starting API server at: http://localhost:38867
At that point the process ends.
The only output warnings are the deprecation of Python 2 (but that's still required according to https://cloud.google.com/appengine/docs/standard/tools/local-devserver-command?tab=python) and the one regarding DATASTORE_EMULATOR_HOST
.
If I run echo $?
(get the exit code) I get 245
.
dev_appserver.py
will crash regardless of whether the Datastore emulator was started with:
./datastore.sh
Turning the Datastore Emulator off by simply setting --support_datastore_emulator=false
in gae.sh
starts the application perfectly well, but of course without Datastore emulation, so all related functionality will fail at runtime.
Upvotes: 2
Views: 506
Reputation: 6263
Start the datastore emulator. After the emulator is running, open a new shell and then run dev_appserver.py in the new shell.
My experience with datastore emulator is that you need to open a new shell after starting the emulator/setting the environment variables for the its environment variables to apply.
Upvotes: 0