I have a problem with my Python function app.
I admit I don’t try a lot of things but I just don’t understand why it’s not working.
Both the class and the function exist in ‘Script1.py’.
In this Microsoft doc, it’s said I need to use absolute import and create a ”’init.py”’ file. I tried to import like this :
from shared.Script1 import class1
and like this :
from shared import Script1
Both of them do not work. And if I don’t import my files in a try/except block, I get the worst message ‘No HTTP triggers found’.
My project :
|- function_app.py
|- host.json
|- local.settings.json
|- requirements.txt
|- shared
||- __init__.py
||- Script1.py
||- Script2.py
function_app.py :
import azure.functions as func
import logging
try:
from shared.Script1 import class1
except ImportError as e:
logging.error(f"Erreur d'importation : {e}")
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
@app.function_name(name="httpTrigger1")
@app.route(route="route")
def route(req: func.HttpRequest) -> func.HttpResponse:
caller = req.params.get('caller')
accessPath = req.params.get('accessPath')
code, response = class1.function1(accessPath, caller)
return func.HttpResponse(body=response, status_code=code)
Logs Azure function :
Result: Failure Exception: NameError: name 'class1' is not defined
2
Answers
I found my problem and it was not what I expected. One parameter from 'host.json' needed to be changed.
Initial 'host.json' version :
Thanks to @Ikhtesam, I tried to create a new functions app and I discovered the difference with a brand new 'host.json' file with a different construction :
New 'host.json' version :
I have the below folder structure and following codes in the respective files.
function_app.py–
script1.py
–Then I executed my function by clicking fn + f5 or func host start and got the expected response.