2

I'm trying to deploy several Cloud Functions which are all in the same repository but in different python files.

Basically, I have those two functions

my_repo
- function_1_folder
  - function_1.py
  - requirements.txt
  - .gcloudignore
- function_2_folder  
  - function_2.py
  - requirements.txt
  - .gcloudignore

Inside function_1.py I have function_1() which I want to deploy to my cloud function called function1 (note there is no underscore here), and same for function_2.

I go to function_1_folder and I have specified an entry point (--entry-point function_1) but I get a "missing main.py" error.

How can I specify both python filename and function name (if possible) ? Will gcloud also deploy the requirements.txt which is needed to install the packages my function depends on ?

Thank you

1 Answer 1

5

You can't name the source file arbitrarily. It's part of the structuring requirements that your Python source file should always be named main.py. This is how your directory should look like:

my_repo
 - function_1_folder
   - main.py
   - requirements.txt
   - .gcloudignore
 - function_2_folder  
   - main.py
   - requirements.txt
   - .gcloudignore

The flag --entry-point is used to specify the name of the function on your source file. For example (as HTTP):

main.py

def function_1(request):
    return 'Hello World'

Run this command inside function_1_folder:

gcloud functions deploy function1 --entry-point function_1 --runtime python37 --trigger-http 

To answer your final question, requirements.txt is included along with your main.py and is a valid configuration. Those dependencies will be installed during build time.

As additional reference, see: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/functions

Sign up to request clarification or add additional context in comments.

1 Comment

In addition to the 'main.py' you can have an arbitrary number of other python files (ie. 'file1.py', 'file2,py', etc.) in the same directory along with the 'main.py'. Those modules can be imported into (and used in) the 'main.py' in an ordinary way. On top of that any other files (i.e. json) in that directory can be used as well. Those file can be read (and used) from inside the code as ordinary files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.