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

11,033

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
11,033
Jed
Author by

Jed

About Me I'm an Associate Director of Data Science with a strong background in industrial businesses. I worked in manufacturing for ~10 years, then transitioned to work on Advanced Analytics, Machine Learning, Data Science projects in the industrial & supply-chain spaces as well.

Updated on June 07, 2022

Comments

  • Jed
    Jed almost 2 years

    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 function edit window screenshot

    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.

  • Jed
    Jed over 5 years
    For some reason, moving main.py to a PACKAGE folder, then using the following command worked while the other options didn't: gcloud functions deploy NAME --source https://source.developers.google.com/projects/PROJECT_ID/rep‌​os/REPOSITORY_ID/mov‌​eable-aliases/BRANCH‌​_NAME/paths/PACKAGE/ --trigger-topic TOPIC
  • Jed
    Jed over 5 years
    Any idea why I was not able to successfully set this function up using the web-based GUI for GCP Functions?
  • Ayyappa
    Ayyappa over 3 years
    "Instead, create a zip file from main.py and requirement.txt and package." - Kudos!