This guide walks you through setting up a development environment for working with Google Cloud Platform (GCP) using Python.
This guide will help you:
- Set up a local development environment for GCP projects using Python.
- Develop a Python Flask application locally.
- Containerize your application for efficient deployment.
- Test your application locally.
- Deploy your application to Google Cloud Run.
- A Bash shell (
bash
) - Git (
git
) - curl (
curl
) - Google Cloud CLI (
gcloud
) - Python 3 (
python3
) - Docker (
docker
) or Podman (podman
) (for containerized development)
Clone this Git repository:
git clone "https://github.com/Cyclenerd/gcp-py.git"
cd "gcp-py"
The Google Cloud CLI lets you manage and interact with GCP services from your terminal.
To install, use the packet manager (recommended) of your operating system or follow the official guide.
macOS:
brew install --cask google-cloud-sdk
Debian/Ubuntu:
sudo apt install apt-transport-https ca-certificates gnupg curl
curl -fsSL "https://packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o "/usr/share/keyrings/cloud.google.gpg"
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a "/etc/apt/sources.list.d/google-cloud-sdk.list"
sudo apt update
sudo apt install google-cloud-cli
To initialize the gcloud CLI, run gcloud init
:
gcloud init
Podman is an open-source, OCI-compliant container management tool that offers a Docker-like experience without the need for a daemon. This makes it a secure and lightweight alternative for managing containers.
If you prefer to use Docker instead of Podman, simply replace
podman
withdocker
.
To install, use the packet manager (recommended) of your operating system or follow the official guide.
macOS:
brew install podman
brew install --cask podman-desktop
Debian/Ubuntu:
sudo apt install podman
After installing, you need to create and start your first Podman machine:
podman machine init
podman machine start
You can then verify the installation information using:
podman info
This section shows you how to bring up an environment with the correct dependencies installed.
First, follow the official guide so that you have a working virtual environment and pip3
installed.
python3 -m venv .venv
source .venv/bin/activate
Once you have created and activated a virtual environment, install the dependencies listed in requirements.txt
:
pip3 install -r requirements.txt
Application Default Credentials (ADC) provide credentials to call Google APIs.
Use the gcloud auth application-default login
command to manage them on your machine.
Avoid working with service accounts and account account keys. Service account keys could pose a security risk if compromised.
To use your own user credentials:
gcloud auth application-default login
For manual authentication (no web flow):
gcloud auth application-default login --no-launch-browser
Learn More: User credentials provided by using the gcloud CLI
With the tools installed, you can start developing.
Here's (main.py
) a simple Python Flask application that lists Google Cloud Storage buckets in your project:
export "GOOGLE_CLOUD_PROJECT=[YOUR-GOOGLE-CLOUD-PROJECT-ID]"
python3 main.py
Make sure to replace [YOUR-GOOGLE-CLOUD-PROJECT-ID]
with your actual project ID.
Test command:
curl "http://localhosy:8080/buckets"
This section demonstrates building a container with your Python application.
Build:
podman build --tag "gcp-py:test" .
Note to Mac users with Apple Silicon (M1, M2, ...) CPUs: Cloud Run is Intel-based. Therefore, create and test the container with X86 (linux/amd64).
podman build --platform "linux/amd64" --tag "gcp-py:test" .
Running the container directly will result in an authentication error:
[...]
File "/usr/local/lib/python3.13/site-packages/google/auth/_default.py", line 693, in default
raise exceptions.DefaultCredentialsError(_CLOUD_SDK_MISSING_CREDENTIALS)
google.auth.exceptions.DefaultCredentialsError: Your default credentials were not found. To set up Application Default Credentials, see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.
To fix this:
- Export your
GOOGLE_APPLICATION_CREDENTIALS
environment variable. This variable points to your credential file location on your machine (usually~/.config/gcloud/application_default_credentials.json
). - Set the
GOOGLE_APPLICATION_CREDENTIALS
variable inside the container. - Set the
GOOGLE_CLOUD_QUOTA_PROJECT
andGOOGLE_CLOUD_PROJECT
variable inside the container.
Here's the command with explanations:
export GOOGLE_APPLICATION_CREDENTIALS="$HOME/.config/gcloud/application_default_credentials.json"
export GOOGLE_CLOUD_PROJECT="[YOUR-GOOGLE-CLOUD-PROJECT-ID]"
podman run \
-v "$GOOGLE_APPLICATION_CREDENTIALS:/tmp/adc.json:ro" \
-e "GOOGLE_APPLICATION_CREDENTIALS=/tmp/adc.json" \
-e "GOOGLE_CLOUD_QUOTA_PROJECT=$GOOGLE_CLOUD_PROJECT" \
-e "GOOGLE_CLOUD_PROJECT=$GOOGLE_CLOUD_PROJECT" \
-e "PORT=8080" \
-p "8080:8080" \
gcp-py:test
The --volume
(-v
) flag injects the credential file into the container (assumes you have already set your GOOGLE_APPLICATION_CREDENTIALS
environment variable on your machine).
The --environment
(-e
) flag sets the GOOGLE_APPLICATION_CREDENTIALS
variable inside the container.
Learn More: Set the quota project using an environment variable and Test locally.
Now lets deploy this Python Flask application to Google Cloud Run:
gcloud run deploy "gcp-py" \
--source="." \
--project="$GOOGLE_CLOUD_PROJECT" \
--region="europe-west1" \
--quiet
Test command:
curl "https://[YOUR-CLOUD-RUN].run.app/buckets" \
-H "Authorization: bearer $(gcloud auth print-identity-token)"
Make sure to replace [YOUR-CLOUD-RUN]
with your actual Google Cloud Run URL.
Once you have familiarized yourself with the basics, you can expand your knowledge with the following helpful documents: