0

In my project I have the following structure:

- apis
    - api_1
    - api_2 
- function_1
- function_2

In function_1.main.py I have the following:

from flask import Flask
import logging
from ..apis.api_1 import api

APP = Flask(__name__)

admin = api()


@APP.route("/")
def example(request):
    user = admin.get_user('username')    
    return "Hello... %s!" % user['name']


if __name__ == "__main__":
    APP.run(host="127.0.0.1", port=8080, debug=True)

Locally, this runs fine. When I go to deploy this file as a GCF, I get the error OperationError: code=3, message=Function failed on loading user code. Error message: Code in file main.py can't be loaded because of the from ..apis.api_1 import api import. How can I bundle just the api_1 code when deploying as a GCF?

user1222324562
  • 965
  • 2
  • 9
  • 24

1 Answers1

0

What you have in the function_1.main.py file is a Flask application, not suitable for a Google Cloud Function.

A python cloud function should literally be a python function taking as arguments a request (if it's a HTTP function) or data and context (if it's a Background function) and returning a response.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • In their http example [they use flask.](https://cloud.google.com/functions/docs/writing/#http_functions) – user1222324562 Jun 17 '19 at 16:22
  • @user1222324562 They use Flask escape utility. The `request` object that is passed to the function is a `Flask.Request` object, but you don't have access to the underlying Flask application, and a Flask application that you build cannot use a request from another application instance – Luiz Ferraz Jun 17 '19 at 17:29
  • @LuizFerraz I see, thank you for the information. However, this is not the issue I am having. I am having the issue of bundling local dependencies when deploying to GCF. `function_1` and `function_2` both use the same apis in `/apis`. I could duplicate the code and drop everything from `apis/` into both function folders [to follow this](https://cloud.google.com/functions/docs/writing/specifying-dependencies-python#packaging_local_dependencies) but that makes developing much more painful – user1222324562 Jun 17 '19 at 18:01
  • @user1222324562 one thing you could try would be to symlink the `apis` directory instead of copying. But I'm not sure if that technique works for the CF deployments (it works for the standard environment GAE app deployments, see https://stackoverflow.com/a/34291789/4495081). – Dan Cornilescu Jun 18 '19 at 03:25