- Create a Python app in the App Engine flexible environment
- Before you begin
- Additional prerequisites
- App Engine locations
- Download the Hello World app
- Run Hello World on your local machine
- Mac OS / Linux
- Windows
- Deploy and run Hello World on App Engine
- Common gcloud command flags
- Clean up
- What’s next
- Learn the whole platform
- Learn about the App Engine flexible environment
- Hello World code review
- main.py
- app.yaml
- requirements.txt
- Python on Google App Engine
- Choose your preferred environment
- Standard environment
- Flexible environment
- Choosing an environment
- Try it for yourself
Create a Python app in the App Engine flexible environment
The REGION_ID is an abbreviated code that Google assigns based on the region you select when you create your app. The code does not correspond to a country or province, even though some region IDs may appear similar to commonly used country and province codes. For apps created after February 2020, REGION_ID .r is included in App Engine URLs. For existing apps created before this date, the region ID is optional in the URL.
Python 3.8, 3.9, 3.10, and 3.11 are now generally available. These versions require you to specify an operating system version in your app.yaml . Learn more.
This quickstart demonstrates how to create and deploy an app that displays a short message. The sample application uses the latest version of Python 3. You can use the same code sample for Python runtime version 3.8 and later, by specifying the version in your app.yaml .
Code samples for Python 3.7 version and earlier are available in Github.
Before you begin
- Sign in to your Google Cloud account. If you’re new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
- In the Google Cloud console, on the project selector page, select or create a Google Cloud project. Note: If you don’t plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project. Go to project selector
- Make sure that billing is enabled for your Google Cloud project.
- Enable the Cloud Build API. Enable the API
- Install the Google Cloud CLI.
- To initialize the gcloud CLI, run the following command:
Additional prerequisites
gcloud app create --project=[YOUR_PROJECT_ID]
- Run the following command to install the gcloud component that includes the App Engine extension for Python:
gcloud components install app-engine-python
App Engine locations
App Engine is regional, which means the infrastructure that runs your apps is located in a specific region, and Google manages it so that it is available redundantly across all of the zones within that region.
Meeting your latency, availability, or durability requirements are primary factors for selecting the region where your apps are run. You can generally select the region nearest to your app’s users, but you should consider the locations where App Engine is available as well as the locations of the other Google Cloud products and services that your app uses. Using services across multiple locations can affect your app’s latency as well as its pricing.
You cannot change an app’s region after you set it.
Note: Two locations, which are called europe-west and us-central in App Engine commands and in the Google Cloud console, are called europe-west1 and us-central1 , respectively, elsewhere in Google documentation.
If you already created an App Engine application, you can view its region by doing one of the following:
- Run the gcloud app describe command.
- Open the App Engine Dashboard in the Google Cloud console. The region appears near the top of the page.
This quickstart demonstrates the latest Python 3 app written with the Flask web framework that can be deployed to App Engine. Although this sample uses Flask, you can use any web framework that satisfies the requirements above. Alternative frameworks include Django, Pyramid, Bottle, and web.py.
Download the Hello World app
We’ve created a simple Hello World app for App Engine so you can quickly get a feel for deploying an app to the Google Cloud.
- Clone the Hello World sample app repository to your local machine.
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/appengine/flexible/hello_world
Run Hello World on your local machine
To run the Hello World app on your local computer:
Mac OS / Linux
python3 -m venv env
source env/bin/activate
cd YOUR_SAMPLE_CODE_DIR
pip install -r requirements.txt
Note: If you are using Cloud Shell, in the toolbar, click Web Previewand select Preview on port 8080 instead.
Windows
Note: These instructions describe how to set up a virtual environment in Python 3. For Python 2 apps, use virtualenv to set up a virtual environment.
Use PowerShell to run your Python packages.
- Locate your installation of PowerShell.
- Right-click on the shortcut to PowerShell and start it as an administrator.
- Create an isolated Python environment.
python -m venv env
.\env\Scripts\activate
cd YOUR_SAMPLE_CODE_DIR
pip install -r requirements.txt
The Hello World message from the sample app displays on the page. In your terminal window, press Ctrl+C to exit the web server.
Deploy and run Hello World on App Engine
- Deploy the Hello World app by running the following command from the hello_world directory:
Common gcloud command flags
- Include the —version flag to specify an ID that uniquely identifies that version of your app, otherwise one is generated for you. Example: —version [YOUR_VERSION_ID]
- Include the —project flag to specify an alternate Google Cloud project ID to what you initialized as the default in the gcloud tool. Example: —project [YOUR_PROJECT_ID]
gcloud app deploy --version pre-prod-5 --project my-sample-app
To learn more about deploying your app from the command line, see Testing and Deploying Your App . For a list of all the command flags, see the gcloud app deploy reference.
This time, the page that displays the Hello World message is delivered by a web server running on an App Engine instance.
Congratulations! You’ve deployed your first App Engine app to the App Engine flexible environment!
If you encountered any errors deploying your application, check the troubleshooting tips.
See the following sections for information about cleaning up as well as links to possible next steps that you can take.
Clean up
To avoid incurring charges, you can delete your Google Cloud project to stop billing for all the resources used within that project.
- Caution: Deleting a project has the following effects:
- Everything in the project is deleted. If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you’ve done in the project.
- Custom project IDs are lost. When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an appspot.com URL, delete selected resources inside the project instead of deleting the whole project.
- In the Google Cloud console, go to the Manage resources page. Go to Manage resources
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
What’s next
Learn the whole platform
Now that you know what it’s like to develop and deploy App Engine apps, you can explore the rest of Google Cloud. You already have the Google Cloud CLI installed which gives you the tools to interact with products like Cloud SQL, Cloud Storage, Firestore, and more.
Learn about the App Engine flexible environment
Here are some topics to help continue your learning about App Engine:
Hello World code review
Hello World is the simplest possible App Engine app, as it contains only one service, has only one version, and all of the code is located within the app’s root directory. This section describes each of the app files in detail.
main.py
The Hello World app is a basic one-file Flask app.
from flask import Flask app = Flask(__name__) @app.route("/") def hello() -> str: """Return a friendly HTTP greeting. Returns: A string with the words 'Hello World!'. """ return "Hello World!" if __name__ == "__main__": # This is used when running locally only. When deploying to Google App # Engine, a webserver process such as Gunicorn will serve the app. app.run(host="127.0.0.1", port=8080, debug=True)
app.yaml
The app.yaml file describes the following configuration for your app:
- Sets env: flex , indicating your app uses the App Engine flexible environment.
- Specifies the runtime used by the app. If you exclude the runtime_version setting, App Engine uses the latest Python version as the default version for your app.
- The entrypoint tells App Engine how to start the app. This app uses gunicorn to serve the Python app as an alternative to Flask’s development server (used when running locally). The $PORT variable is set by App Engine when it starts the app. For more information about entrypoint , see App startup.
# Copyright 2021 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. runtime: python env: flex entrypoint: gunicorn -b :$PORT main:app runtime_config: operating_system: ubuntu22 # This sample incurs costs to run on the App Engine flexible environment. # The settings below are to reduce costs during testing and are not appropriate # for production use. For more information, see: # https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml manual_scaling: instances: 1 resources: cpu: 1 memory_gb: 0.5 disk_size_gb: 10
- For more information on how the Python runtime works, see The Python runtime.
- For more details about how to design your app to take advantage of versions and services, see An overview of App Engine.
- For more details about the configuration settings for App Engine, see Configuring your app with app.yaml.
requirements.txt
requirements.txt and the Python package manager pip declare and install application dependencies. Hello World requires Flask, a web framework, and Gunicorn, a WSGI server.
requirements.txt defines the libraries that will be installed both locally and when deploying to App Engine.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-07-24 UTC.
Python on Google App Engine
App Engine offers you a choice between two Python language environments. Both environments have the same code-centric developer workflow, scale quickly and efficiently to handle increasing demand, and enable you to use Google’s proven serving technology to build your web, mobile and IoT applications quickly and with minimal operational overhead. While the two environments have a lot in common, they differ in a few important ways.
Choose your preferred environment
Standard environment
- Support for Python 2.7, Python 3.7, Python 3.8, Python 3.9, Python 3.10, and Python 3.11
- The Python 3.11 runtime is capable of running any framework, library, or binary
- Optimized to scale nearly instantaneously to handle huge traffic spikes
- Free tier
- The Python 2.7 runtime is not recommended because Python 2 is no longer supported by the community
- Ubuntu OS
Flexible environment
- Open source runtimes capable of running any framework, library, or binary
- Greater CPU and memory instance types
- Can access resources in the same Compute Engine network
- Python 3.6, Python 3.7, Python 3.8, Python 3.9, Python 3.10, and Python 3.11
- No free tier. Application always has a minimum number of running instances. Most cost-effective for applications that serve traffic continuously
- Ubuntu OS
Choosing an environment
You can run an App Engine application in two environments, the standard environment and the flexible environment. See a summary of differences between the two environments.
Try it for yourself
If you’re new to Google Cloud, create an account to evaluate how App Engine performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.