<img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=2634489&amp;fmt=gif">

Application Development, AI and Machine Learning, Containers and Kubernetes

How to Run Python on Google Cloud

By Jennifer Marsh | September 20, 2021

The Big Question - Can You Use Python in the Cloud?


Python is an excellent tool for application development. It offers a diverse field of use cases and capabilities, from machine learning to big data analysis. This versatility has allowed the creation of a real niche for Python cloud computing. And now, as DevOps becomes more and more cloud-based, Python is also making its way into cloud computing as well.

However, that's not to say that running Python can't come with its own set of challenges. For example, applications that perform even the simplest tasks need to run 24/7 for users to get the most out of their capabilities, but this can take up a lot of bandwidth - literally.

Python can run numerous local and web applications, and it's become one of the most common for scripting automation to synchronize and manipulate data in the cloud. DevOps, operations, and developers use Python as a preferred language, mainly for its many open-source libraries and add-ons. It's also the second most common language used on GitHub repositories. 

Today we're talking about running Python scripts on Google Cloud and deploying a basic Python application to Kubernetes.



How to Use Google Cloud for Programming


Businesses all over the world can benefit from cloud options. Both cloud-native and hybrid structures have technological benefits like data warehouse modernization and levels of security compliance that help fortify the development process and run continuously. But running code on Google Cloud requires a proper setup and a migration strategy - specifically a Kubernetes migration strategy - if you intend to orchestrate containerization.

Generally speaking, however, any code deployed in Google Cloud is run by a virtual machine (VM). Kubernetes, Docker, and even Anthos make application modernization possible for large applications. In the case of smaller scripts and deployments, a customizable VM instance is adequate for running Python script on Google Cloud and determining processor size, the amount of RAM, and even the operating system of choice for running applications. 


1. Check the Requirements for Running Python Script on Google Cloud


Before you can work with Python in Google Cloud, you need to set up your Python development environment. After that, you can code for the Python cloud environment using your local device, but you must install the Python interpreter and the SDK. The complete list of requirements includes:

  • Install the latest version of Python.

  • Use venv to isolate dependencies.

  • Install your favorite Python editor. One popular Python Integrated Development Environment (IDE) is PyCharm.

  • Install the Google Cloud SDK (gcloud CLI) for Python to access Google Cloud

  • Install any third-party libraries that you prefer.


2. Google Container Registry and Code Migration


To begin scheduling Python scripts on Google Cloud, teams must first migrate their code to the VM instance. For Python VM setup, many experts recommend using Google Container Registry for storing Docker images and the Dockerfile.

First, you must enable the Google Container Registry. The Container Registry requires billing set up on your project, which can be confirmed on your dashboard. Since you already have the Cloud SDK installed, use the following Python gcloud command to enable the registry:

gcloud services enable containerregistry.googleapis.com

If you have images from third-party images, Google provides step-by-step instructions with a sample script that will migrate to the Registry. You can do this for any Docker image that you store on third-party services, but you may want to create new projects in Python that will be stored in the cloud.


3. Creating a Python Container Image


After you create a Python script, you can create an image for it. A Docker image is a text file that contains the commands to build, configure, and run the application. The following Docker example shows you the content of a Dockerfile used to build and image:

# syntax=docker/dockerfile:1

FROM python:3.8-slim-buster


COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "-m" , "flask", "run", "--host="]

After you create the image, you can now build it. Use the following command to build it:

$ docker build --tag python-docker

The --tag option tells Docker what to name the image. You can read more about creating and building Docker images here.

After the image is created, you can move it to the cloud. You must have a project set up in your Google Cloud Platform dashboard and be authenticated before migrating the container. The following command migrates the image to Google Cloud Platform:

gcloud build submit

The above basic commands will migrate a sample Python image, but full instructions can be found in the Google Cloud Platform documentation.


4. Initiating the Docker Push to Create a Google Cloud Run Python Script


Once the Dockerfile has been uploaded to the Google Container Registry and the Python image has been created, it's time to initiate the Docker push command to finish the deployment and prepare the storage files. A Google Cloud run Python script requires creating two storage files before a developer can claim the Kubernetes cluster and deploy it to Kubernetes.

The Google Cloud Run platform has an interface to deploy the script and run it in the cloud. Open with the Cloud Run interface, click "Create Service - from the menu and configure your service. Next, select the container pushed to the cloud platform and click "Create - when you finish the setup.


5. Deploying the Application to Kubernetes


The final step to schedule a Python script on Google Cloud is to create the service file and the deployment file. Kubernetes is common in automating Docker images and deploying them to the cloud. Orchestration tools use a language called YML to set up configurations and instructions that will be used to deploy and run the application. 

Once the appropriate files have been created, it's time to use kubectl to initiate the last stage of the final stage to run Python on Google Cloud. Kubectl is a command-line tool that makes running commands against Kubernetes like deployments, inspections, and log visibility. It's an integral step to ensure the Google Cloud run Python script runs efficiently in Kubernetes and the last leg of the migration process.

To deploy a YML file to Kubernetes, run the following command:

$ kubectl create -f example.yml

You can verify that your files deployed by running the following command:

$ kubectl get services

Extra Credit

  1. The Easiest Way to Run Python In Google Cloud (Illustrated) 

  2. Running a Python Application on Kubernetes

  3. Running a Python application on Kubernetes

  4. Google Cloud Run - Working with Python

Recent Articles

Google Cloud Strategy

AI Cheat Sheet

AI is no more and no less the drive to create robots with human minds so they can do everything we do and more. Use this cheat sheet to help decode the space.
By Leah Zitter
AI and Machine Learning

CarCast with Bruno Aziza: What Makes Us Better Than AI?!

Unlock the secrets of human cognitive superiority over AI in this compelling CarCast with Bruno Aziza and Kenneth Cukier.
By Bruno Aziza
AI and Machine Learning

CarCast with Bruno Aziza: The Metrics You CAN'T Afford To ...

Discover essential CEO metrics: Rule of 40, CAC Ratio, NRR/GRR, and more. Optimize your business for success now!
By Bruno Aziza