Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying to Google Cloud Function: Error message: 'main'

I deploy a Python app to Google Cloud Functions and got this very vague error message:

$ gcloud functions deploy parking_photo --runtime python37  --trigger-http
Deploying function (may take a while - up to 2 minutes)...failed.                   
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. Error message: 'main'

I don't know what is wrong. Searching around gives no result. Anyone can help?

I believe my code layout is correct:

$ tree
.
├── main.py
├── poetry.lock
├── pyproject.toml
├── README.rst
├── requirements.txt
└── tests
    ├── __init__.py
    └── test_burrowingowl.py

My main.py file has a function that matches the function name:

import operator
from datetime import datetime

import logbook
from flask import Request, abort, redirect
from pydantic import ValidationError
from pydantic.dataclasses import dataclass
from google.cloud import storage
from pytz import timezone


logger = logbook.Logger(__name__)
storage_client = storage.Client()


@dataclass
class Form:
    bucket: str = ...
    parkinglot: str = ...
    space_id: int = ...
    tz: str = ...


def parking_photo(request: Request):
    # Some code
    return

Update

Thank you for the answers. This topic is out of my sight, when I didn't receive notification from StackOverflow for a while.

Last year, I fixed it by just dropping use of dataclass. At that time, Google claimed to support Python 3.7 but actually not, that is why dataclass didn't work.

When you tried to reproduce this issue, maybe Google already fix the Python 3.7 compatibility.

like image 716
hongquan Avatar asked Jul 24 '26 20:07

hongquan


1 Answers

Most likely your function is raising a FileNotFound error, and Cloud Functions interprets this as main.py not existing. A minimal example that will cause the same error:

$ cat main.py
with open('missing.file'):
    pass

def test(request):
    return 'Hello World!'

You should check to make sure that any files you're trying to open are included with your function. You can try/except for this error and log a message to figure it out as well.

like image 68
Dustin Ingram Avatar answered Jul 26 '26 09:07

Dustin Ingram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!