简体   繁体   中英

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

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" and my function runs fine when I copy and paste the code into the GCP Function "Inline editor". When I switch to the "Cloud Source repository" option, I can tell that it is reading from that repository; I worked through other errors prior to this one. However, after resolving prior/other issues, now I'm getting this error:

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

my files are in a structure similar to this, with main.py in the root directory:

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

It's reading fine from requirements.txt (some of the prior errors came from that reading process), but why is it not reading from main.py ? My setup in the GCP Function looks like this:

Google Cloud 函数编辑窗口截图

I have tried to move main.py to another directory in the project and setting the "Directory with source code" to that directory, but that gave me an error saying that it couldn't find that directory. Any constructive ideas?

Edit / Additional Information

I am using a branch from my repository other than master , and I am using a Google Cloud Pubsub topic trigger for this function.

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 create a Zip file from the CodeFolder .创建从一个Zip文件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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM