Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions with Multiple Python Files

I am unable to import my other python file in the init.py file. I have tried importing that according to the same way as mentioned in the documentation.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#import-behavior

This is my init.py file:

    import logging

    import azure.functions as func
    from . import test


    def main(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
        print("test")
        num = sum_num(2, 3)
        print(num)
        name = req.params.get('name')
        if not name:
            try:
                req_body = req.get_json()
            except ValueError:
                pass
            else:
                name = req_body.get('name')

        if name:
            return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
        else:
            return func.HttpResponse(
                "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
                status_code=200
            )

this is my test.py file with very basic sum function:

    def sum_num(val1, val2):
        print("inside function")
        return val1 + val2

Please help me out how to import this sum_num function in my init.py file.

the folder structure is also fine as you can see:

Error i am getting in the logs

like image 974
Mueez Ahmad Avatar asked Sep 17 '25 12:09

Mueez Ahmad


1 Answers

You have already imported the module using

from . import test

so in order to access the function from the module you should use

num = test.sum_num(2, 3)

Alternatively you can import only the function from the module.

from .test import sum_num
like image 167
Abdul Niyas P M Avatar answered Sep 20 '25 03:09

Abdul Niyas P M