Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

cloud run with python within vscode

I am trying to run a very simple python script that prints "Hello" on the cloud code extension within vscode. 

I have already created a docker file that calls a script (cloud_trial.py) which contains the printing message:

Screenshot 2022-12-16 at 12.18.15.png

 When I click on the cloud run on local emulator button, the following output pane appears. 

Screenshot 2022-12-16 at 12.18.59.png

This starts updating a lot of files that are within my project directory (with errors) but it never prints out the message from the script. It seems that it does not understand how to just run the single .py file.

Why can this happen? Is there any good tutorial on how to use vscode for python deployment on Google Cloud?

Solved Solved
0 2 4,665
1 ACCEPTED SOLUTION

Hi @luisig,
 
Welcome to Google Cloud Community!
 
It looks like you are trying to use the Cloud Code extension for Visual Studio Code to run a Python script in a Docker container on Google Cloud.
 
To run a Python script using the Cloud Code extension, you will need to do the following:
 
1. Install the Cloud Code extension in Visual Studio Code.
2. Create a Python script and save it in your project directory.
3. Create a Dockerfile that specifies how to build the Docker image for your script. The Dockerfile should include a command to run the Python script when the container is started.
4. In the Cloud Code extension, click the Cloud Run button in the activity bar to open the Cloud Run view.
5. In the Cloud Run view, click the "Create a new deployment" button. This will open a dialog where you can specify the details of your deployment.
6. In the dialog, select "Docker" as the runtime and choose the Dockerfile you created in step 3.
7. Click the "Deploy" button to deploy your script to Cloud Run.
 
The configuration file you provided is for running a Docker container, not for running a Python script. To run a Python script using the Cloud Code extension, you can use the following configuration:
 
{
    "type": "python",
    "request": "launch",
    "name": "Run Python script",
    "pythonPath": "${config:python.pythonPath}",
    "module": "cloud_trial",
    "cwd": "${workspaceFolder}"
}
 
This configuration will run the script cloud_trial.py using the Python interpreter specified in the pythonPath field. The cwd field specifies the current working directory, which should be set to the root of your project.
 
It's also worth noting that the Cloud Code extension provides integration with Google Cloud Platform, including the ability to deploy applications to Google App Engine and Google Kubernetes Engine. You can find more information about using the Cloud Code extension in the documentation: https://cloud.google.com/code/docs/vscode/quickstart
 
Thank you

View solution in original post

2 REPLIES 2

Hi @luisig,
 
Welcome to Google Cloud Community!
 
It looks like you are trying to use the Cloud Code extension for Visual Studio Code to run a Python script in a Docker container on Google Cloud.
 
To run a Python script using the Cloud Code extension, you will need to do the following:
 
1. Install the Cloud Code extension in Visual Studio Code.
2. Create a Python script and save it in your project directory.
3. Create a Dockerfile that specifies how to build the Docker image for your script. The Dockerfile should include a command to run the Python script when the container is started.
4. In the Cloud Code extension, click the Cloud Run button in the activity bar to open the Cloud Run view.
5. In the Cloud Run view, click the "Create a new deployment" button. This will open a dialog where you can specify the details of your deployment.
6. In the dialog, select "Docker" as the runtime and choose the Dockerfile you created in step 3.
7. Click the "Deploy" button to deploy your script to Cloud Run.
 
The configuration file you provided is for running a Docker container, not for running a Python script. To run a Python script using the Cloud Code extension, you can use the following configuration:
 
{
    "type": "python",
    "request": "launch",
    "name": "Run Python script",
    "pythonPath": "${config:python.pythonPath}",
    "module": "cloud_trial",
    "cwd": "${workspaceFolder}"
}
 
This configuration will run the script cloud_trial.py using the Python interpreter specified in the pythonPath field. The cwd field specifies the current working directory, which should be set to the root of your project.
 
It's also worth noting that the Cloud Code extension provides integration with Google Cloud Platform, including the ability to deploy applications to Google App Engine and Google Kubernetes Engine. You can find more information about using the Cloud Code extension in the documentation: https://cloud.google.com/code/docs/vscode/quickstart
 
Thank you

Thanks a lot! I'm still familiarizing with it so that seemed to be the error. I managed to deploy the function and make it run properly. However, I tried to deploy a new one and when I run it on the vscode extension I get this error:

 

Screenshot 2022-12-23 at 12.56.20.png

The launch.json folder looks like this:

 

 

 

{
  "configurations": [
    {
      "name": "Deploy Cloud Function",
      "type": "cloudcode.cloudfunctions",
      "request": "launch",
      "functionName": "scraping",
      "gen": "GEN_2",
      "revision": "scraping-00001-biy",
      "entryPoint": "hello_get",
      "testInput": "",
      "region": "europe-west1",
      "runtime": "python310"
    },
    {
      "name": "Cloud Run: Run/Debug Locally",
      "type": "cloudcode.cloudrun",
      "request": "launch",
      "build": {
        "docker": {
          "path": "Dockerfile"
        }
      },
      "image": "scraping",
      "service": {
        "name": "scraping",
        "containerPort": 8080,
        "resources": {
          "limits": {
            "memory": "256Mi"
          }
        }
      },
      "target": {
        "minikube": {}
      },
      "watch": true,
      "internalConsoleOptions": "neverOpen"
    },
    {
      "name": "Python: Current File",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "justMyCode": true
    },
    {
      "name": "Deploy Cloud Function",
      "type": "cloudcode.cloudfunctions",
      "request": "launch",
      "functionName": "function-scrape",
      "gen": "GEN_2",
      "entryPoint": "hello_get",
      "revision": "function-scrape-00003-vuh",
      "testInput": "",
      "region": "europe-west1",
      "runtime": "python310"
    },
    {
      "name": "Docker: Python - General",
      "type": "docker",
      "request": "launch",
      "preLaunchTask": "docker-run: debug",
      "python": {
        "pathMappings": [
          {
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/app"
          }
        ],
        "projectType": "general"
      }
    }
  ]
}

 

 

Any ideas on why this might be happening? 

You can see that the function status is not correctly deployed:

 

Screenshot 2022-12-23 at 13.02.56.png

Screenshot 2022-12-23 at 13.04.08.png