skip to Main Content

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


  1. Chosen as BEST ANSWER

    I found my problem and it was not what I expected. One parameter from 'host.json' needed to be changed.

    Initial 'host.json' version :

    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          }
        }
      },
      "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[3.*, 4.0.0)"
      }
    }
    

    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 :

    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          }
        }
      },
      "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[4.*, 5.0.0)"
      }
    }
    

  2. I have the below folder structure and following codes in the respective files.

    enter image description here

    function_app.py

    import azure.functions as func
    import logging
    try:
        from shared.script1 import class1
    except ImportError as e:
        logging.error(f"Error : {e}")
    
    app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
    
    @app.route(route="http_trigger")
    def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
        caller = req.params.get('caller')
        accessPath = req.params.get('accessPath')
        response = class1.function1(accessPath, caller)
        return func.HttpResponse(body=response)
    

    script1.py

    class class1:
        @staticmethod
        def function1(access_path, caller):
            # Implement your logic here
            if access_path == "12" and caller == "13":
                return "Access granted for caller1 to path1"
            else:
                return "Access denied"
    
    

    Then I executed my function by clicking fn + f5 or func host start and got the expected response.

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search