Google App Engine Standard Environment Services SDK for Python 3
The `appengine-python-standard` library is the Google App Engine services SDK for Python 3. It provides Python 3 applications running in the App Engine standard environment access to various legacy bundled services and API endpoints that were previously only available on the Python 2.7 runtime, such as Mail, Memcache, NDB, and URL Fetch. The library is actively maintained, with minor, patch, and occasional major releases addressing new features and compatibility.
Warnings
- breaking Migrating from Python 2.7 to Python 3 on App Engine Standard is a significant undertaking. The Python 3 runtime fundamentally differs, deprecating Python 2.7's bundled services model and requiring the use of standard Python libraries or specific SDKs like `appengine-python-standard`. Frameworks like `webapp2` are not supported, necessitating a switch to WSGI-compatible frameworks like Flask or Django.
- gotcha To enable App Engine APIs with this SDK, your `app.yaml` must explicitly include `app_engine_apis: true`, and your WSGI application object (e.g., Flask app) must be wrapped with `google.appengine.api.wrap_wsgi_app()`. Failure to do so will result in API calls failing.
- breaking The standard Python `cgi` module, previously used for handling CGI requests, has been removed in Python 3.13. Applications relying on this module will break. `appengine-python-standard` v1.1.11 and later address this by using `legacy-cgi` for Python versions above 3.9, but explicit dependency might still be needed for certain setups.
- gotcha When using NDB in Python 3 on App Engine Standard, you must import `ndb` from `google.cloud.ndb` and ensure an NDB client context is active for Datastore operations. The Python 2 `google.appengine.ext.ndb` import is deprecated and will not work.
- deprecated Several `app.yaml` attributes from Python 2.7 are deprecated or unsupported in the Python 3 runtime, including `api_version`, `threadsafe`, `libraries`, `builtins`, and `skip_files`. Using them may lead to deployment errors or unexpected behavior.
- gotcha The Mail API's functionality in Python 3 has evolved. While `appengine-python-standard` re-introduces the Mail API via `google.appengine.api.mail`, earlier migration advice suggested using third-party mail providers due to its initial absence in Python 3. Version 2.0.0 adds SMTP fallback, introducing new environment variables for configuration (`APPE_MAIL_SMTP_HOST`, `APPE_MAIL_SMTP_PORT`, etc.).
Install
-
pip install appengine-python-standard
Imports
- wrap_wsgi_app
from google.appengine.api import wrap_wsgi_app
- mail
from google.appengine.api import mail
- urlfetch
from google.appengine.api import urlfetch
- memcache
from google.appengine.api import memcache
- ndb
from google.cloud import ndb
Quickstart
import os
from flask import Flask, request
from google.appengine.api import wrap_wsgi_app, memcache
app = Flask(__name__)
app.wsgi_app = wrap_wsgi_app(app.wsgi_app)
@app.route('/')
def hello():
visits = memcache.incr('visits', initial_value=0)
return f'Hello, App Engine! You have visited this page {visits} times.'
if __name__ == '__main__':
# This is used when running locally. Gunicorn is used for deployment.
# Ensure that `app_engine_apis: true` is set in app.yaml for deployment.
port = int(os.environ.get('PORT', 8080))
app.run(host='127.0.0.1', port=port, debug=True)