UPDATE
I've documented my problem and solution in much more detail here »


I had been trying to deploy my script as a Cloud Run Service. I should've tried deploying it as a Cloud Run Job. The difference is that cloud run services require your script to listen for a port. jobs do not.

Confusingly, you cannot deploy a cloud run job directly from Artifact Registry. You have to start from the cloud run dashboard.

Answer from Ben on Stack Overflow
🌐
Google
docs.cloud.google.com › python › getting started with python on compute engine
Getting started with Python on Compute Engine | Google Cloud Documentation
From Cloud Shell, you can deploy to a single Compute Engine instance virtual machine (VM) which runs your app. You need a way to instruct your instance to download and run your code.
Discussions

google cloud platform - Running basic Python script on GCP - Stack Overflow
I know this is basic but I am really struggling on how to get started and am so so so frustrated. I have developed the basis of my app on my windows pc using juypter notebooks and now want to start More on stackoverflow.com
🌐 stackoverflow.com
Automating Python with Google Cloud
This tutorial is great. I really wanted to learn this. More on reddit.com
🌐 r/Python
30
120
March 28, 2024
Running a python script on Google Cloud Compute Engine - Stack Overflow
For a machine learning task at school I wrote my own MLP network. The data set is quite big, and training takes forever. I was alerted to the option of running my script on the Google Cloud Compute More on stackoverflow.com
🌐 stackoverflow.com
What's the best way to run python scripts on the cloud to process Google Sheets data?
There’s probably 100 ways to do this, but I’ve done something similar using google cloud platform services. I’d use cloud functions to run the script. Use gspread to interact with your sheet. Not sure what the best option is to save the output to google drive, but there are some google client libraries that can probably assist with that. You’ll need to sort out Auth and permissions, which can be a pain but is doable. You can trigger the cloud function on a schedule, or manually, or whatever other triggering mechanism you want to use. Depending on what you are doing, you’d probably stay in the free tier. More on reddit.com
🌐 r/pythontips
9
3
March 16, 2024
🌐
Google
developers.google.com › google workspace › apps script › python quickstart
Python quickstart | Apps Script | Google for Developers
This quickstart guide helps you create a Python command-line application that interacts with the Google Apps Script API using simplified authentication suitable for testing. To run the quickstart, you need Python 3.10.7 or greater, pip, a Google Cloud project, and a Google account with Google ...
🌐
DEV Community
dev.to › njoguu › running-a-python-script-on-google-cloud-in-2023-a-step-by-step-guide-272k
Running a Python Script on Google Cloud in 2023: A Step-by-Step Guide - DEV Community
May 31, 2023 - In this step-by-step guide, we covered the process of creating a VM instance, connecting to it, installing Python, pip, Git, and tmux, and running a Python file from a cloned repository. As the world of technology continues to evolve, Google Cloud remains at the forefront of innovation, providing developers with powerful tools and services to bring their Python applications to life. So, go ahead and unleash the full potential of your Python scripts on Google Cloud.
🌐
Google
codelabs.developers.google.com › codelabs › cloud-run-hello-python3
Hello Cloud Run with Python | Google Codelabs
March 20, 2026 - In the Cloud Shell window, click the Web Preview icon and select Preview on port 8080: This should open a browser window showing the Hello World! message. You can also open another Cloud Shell session (a new terminal tab) by clicking the + icon and sending a web request to the application running ...
🌐
C2cglobal
c2cglobal.com › articles › how-to-run-python-on-google-cloud-1356
How to Run Python on Google Cloud
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.
Find elsewhere
🌐
Reddit
reddit.com › r/python › automating python with google cloud
r/Python on Reddit: Automating Python with Google Cloud
March 28, 2024 -

I just published a tutorial series on how to automate a Python script in Google Cloud using Cloud Functions and/or Cloud Run. Feedback would be great. Thanks!

  • Automating Python with Google Cloud

    • Automating Python with Google Cloud Functions

    • Automating Python with Google Cloud Run

Top answer
1 of 5
39

I finally figured this out so I'll post the same answer on my own post that worked for me here. Using Debian Stretch on my VM. I'm assuming you already uploaded your file(s) to the VM and that you are in the same directory of your script.

  1. Make your script an executable

    chmod +x myscript.py
    
  2. Run the nohup command to execute the script in the background. The & option ensures that the process stays alive after exiting. I've added the shebang line to my python script so there's no need to call python here

    nohup /path/to/script/myscript.py &
    
  3. Logout from the shell if you want

    logout
    

Done! Now your script is up and running. You can login back and make sure that your process is still alive by checking the output of this command:

ps -e | grep myscript.py

If anything went wrong, you can check out the nohup.out file to see the output of your script:

cat nohup.out
2 of 5
9

There is even a simpler approach to to run code in the background in gcp and in every linux terminal: using screen linux

Create a new background terminal window:

screen -S WRITE_A_NAME_OF_YOUR_CHOIC_HERE

now you are in a background window in the terminal. Run your code:

python3 mycode.py

Exit screen with the hotkeys and the job will keep running on the background.

ctrl + A + D

You can close all windows now. If you wanna go back and see what's happening. Log again in your terminal. And tap the following.

screen -ls 

This one will give you the list of the created "windows". Now find yours and tap

screen -r WRITE_NAME_OF_YOUR_WINDOW

And there you have it :D You can find more commands here

🌐
Davidmuraya
davidmuraya.com › blog › python-google-cloud-functions-guide
How to Run Python Scripts in the Cloud with Google Cloud Functions
December 3, 2025 - To run it, you would execute python main.py in your terminal. This approach confirms your logic works and that you are handling environment variables correctly before you deploy. Newer (2nd gen) Cloud Functions are built on top of Cloud Run.
🌐
Anderfernandez
anderfernandez.com › en › blog › automate-python-script-google-cloud
How to automate a Python script on Google Cloud - Ander Fernández
In this post I explain step by step how you can automate a Python script in Google Cloud with Cloud Functions, Cloud Scheduler and Pub/Sub.
🌐
GitHub
github.com › GoogleCloudPlatform › getting-started-python
GitHub - GoogleCloudPlatform/getting-started-python: Code samples for using Python on Google Cloud Platform · GitHub
This repository is the complete sample code for the Python Getting Started on Google Cloud Platform tutorials. Please refer to the tutorials for instructions on configuring, running, and deploying these samples.
Starred by 817 users
Forked by 575 users
Languages   Python 53.7% | Shell 29.7% | HTML 13.5% | Dockerfile 2.2% | Makefile 0.9%
🌐
Reddit
reddit.com › r/dataengineering › which gcp service/process to use to run a python script that generates data and writes it to a storage bucket
r/dataengineering on Reddit: Which GCP service/process to use to run a python script that generates data and writes it to a storage bucket
December 6, 2023 -

This is just me messing about - but I'm wondering what the 'best' (or most common) approach to this might be ? Basically I have a python module that will generate some fake data - and I want to write that to a storage bucket on a cloud scheduled job. So every X minutes it'll generate some synthetic data into some bucket (assume <=1GB of data).

It seems that cloud run / cloud functions are the most obvious for this task, but which one and why (or is there something else more suited to running python code like this?)

Reading some of the documentation for cloud run https://cloud.google.com/run/docs/developing it seems to be geared towards web services (most of the examples seem to use flask...), and I'm not sure that's what I'm after here.

I figured it'd be a fairly easy question for people with GCP experience.

Thanks

🌐
PyPI
pypi.org › project › google-cloud-run
google-cloud-run · PyPI
python3 -m venv <your-env> source <your-env>/bin/activate pip install google-cloud-run · py -m venv <your-env> .\<your-env>\Scripts\activate pip install google-cloud-run · Read the Client Library Documentation for Cloud Run to see other available ...
      » pip install google-cloud-run
    
🌐
Tilburg Science Hub
tilburgsciencehub.com › topics › automation › replicability › cloud-computing › google-colab
Use Google Colab to Run Python in the Cloud - Tilburg Science Hub
Colab is a product from Google Research that allows anybody with an internet connection to write and execute python code through a browser. Colab is free to use, and there is no setup on your computer needed. It is especially useful if you have a slow computer since Google hosts your Jupyter ...