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

前端 未结 5 1171
渐次进展
渐次进展 2021-01-04 16:36

I am trying to implement a Google Cloud function that is based on code in a Git style repository. I have the code linked to Google Cloud Platform\'s \"Source Repositories\"

相关标签:
5条回答
  • 2021-01-04 16:55

    I had a similar (possibly same?) problem. What happened with me is that all my files were in the format:

    1. [zip file name]/main.py
    2. [zip file name/[etc.]

    and the error kept saying it could not find main. I was guessing that this was because it had a parent folder. After looking at zipping into the parent for a bit I determined it wasn't the issue.

    I downloaded the zip of and looked at autogenerated dialogflow cloud function code and noticed they had a "package.json" file. I instead just had a "requirements.txt" file. I copied the package.json file to my source code, editted it for the correct contents, zipped it, and the cloud function compiled correctly.

    package.json looks like:

    {
      "name": "test",
      "description": "testingThings",
      "version": "0.0.1",
      "private": true,
      "license": "Apache Version 2.0",
      "author": "ABCDEFG",
      "engines": {
        "node": "8"
      },
      "scripts": {
        "start": "firebase serve --only functions:test",
        "deploy": "firebase deploy --only functions:test"
      },
      "dependencies": {
        "google-cloud-storage": "",
        "google-cloud-firestore": "",
      }
    }
    

    I believe they use this for deploying the google cloud functions so without it they break.

    0 讨论(0)
  • 2021-01-04 16:57

    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.

    0 讨论(0)
  • 2021-01-04 17:02

    If despite the other answers, you're still not able to make it work, I finally got rid of this error by doing the following:

    • make sure your paths to files are correct (especially json files if you're loading your credentials from them, watch out relative paths)
    • check your requirements.txt file: I was using pipreqs to auto generate it but it did some mistakes ("_" instead of "-" for modules like google-cloud-tasks for instance). I'd recommend writing the requirements.txt yourself based on your imports.

    I hope this helps.

    0 讨论(0)
  • 2021-01-04 17:04

    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

    0 讨论(0)
  • 2021-01-04 17:18

    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.

    0 讨论(0)
提交回复
热议问题