diff --git a/src/functions_framework/__init__.py b/src/functions_framework/__init__.py index 246dcbb2..74329dba 100644 --- a/src/functions_framework/__init__.py +++ b/src/functions_framework/__init__.py @@ -232,11 +232,7 @@ def create_app(target=None, source=None, signature_type=None): global errorhandler errorhandler = app.errorhandler - # 6. Execute the module, within the application context - with app.app_context(): - spec.loader.exec_module(source_module) - - # Handle legacy GCF Python 3.7 behavior + # 6. Handle legacy GCF Python 3.7 behavior if os.environ.get("ENTRY_POINT"): os.environ["FUNCTION_TRIGGER_TYPE"] = signature_type os.environ["FUNCTION_NAME"] = os.environ.get("K_SERVICE", target) @@ -254,6 +250,10 @@ def handle_none(rv): sys.stderr = _LoggingHandler("ERROR", sys.stderr) setup_logging() + # 7. Execute the module, within the application context + with app.app_context(): + spec.loader.exec_module(source_module) + # Extract the target function from the source file if not hasattr(source_module, target): raise MissingTargetException(