Skip to main content

Environment checker: Google App Engine

One of the pieces of logic I use often in my code is checking whether it's running locally or remotely.

The following code will check whether it's running in a local machine or in Google App Engine.

def am_in_appengine() -> bool:
"""
This function detects whether it's running in a local environment or in Google App Engine.

This utilizes the $GAE_ENV env variable. If you have it configured in your local machine, ensure that it doesn't start with: 'standard'.

Returns:
bool: True if running in Google App Engine, False otherwise
"""
# Source https://cloud.google.com/appengine/docs/standard/testing-and-deploying-your-app?tab=python#detecting_application_runtime_environment
return bool(os.getenv("GAE_ENV", "").startswith("standard"))

For instance you would use something like this to better utilize the environment you're in. For example, a common tactic is to let Google's GCP libraries handle authentication, something you can't do while running locally.