Google Cloud Function - Function load error: File main.py that is expected to define function doesn't exist

If you're trying to upload your code zip using a GCS bucket or the file upload function, make sure that you don't zip the folder that contains your code but just the code files.

CodeFolder
├── package
|   ├──script1.py
|   └──script2.py
├── package2
├── ...
├── main.py
└── requirements.txt

Do NOT create a Zip file from the CodeFolder.

Instead, create a zip file from main.py and requirement.txt and package.

Source


Are you redeploying the function after pushing new commits? You'll need to do something like:

gcloud functions deploy NAME \
  --source https://source.developers.google.com/projects/PROJECT_ID/repos/REPOSITORY_ID/moveable-aliases/master/paths/SOURCE \
  TRIGGER

See https://cloud.google.com/functions/docs/deploying/repo for more details.


Is your cloud function doing anything before it reaches the entry point function you've assigned? If any unhandled exceptions happen, GCF will not reach the entry point function and throw this error. For example:

class SomeClass:
    def __init__(self):
        raise ValueError

err = SomeClass()

def main(event, context):
    pass

Will raise the same error: Function load error: File main.py that is expected to define function doesn't exist. That's because your entry point function is never reached. Review the code that runs before your function is defined and you'll likely find something amiss.