azureazure-functions

Azure Function FunctinoBuilder Exception


I'm developing an azure function in python using python3.10.5, with the version of azure-functions==1.20.0. When running it locally or in Azure it gives me a Exception:

TypeError: <azure.functions.decorators.function_app.FunctionBuilder object at 0x7f5015b52110> is not a module, class, method, or function.

My init.py is:

import azure.functions as func
import logging
import MegaVoceroTranslator.megaVoceroTranslator as megaVoceroTranslator
import shutil

def clean_up_tmp_folder():
    tmp_path = 'tmp'
    try:
        shutil.rmtree(tmp_path)
        logging.info(f"Successfully deleted {tmp_path}")
    except FileNotFoundError:
        logging.info(f"{tmp_path} does not exist")
    except Exception as e:
        logging.error(f"Error deleting {tmp_path}: {str(e)}")

app = func.FunctionApp()
        
@app.function_name(name="MegaVoceroTranslator")
@app.route(route="MegaVoceroTranslator")
async def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    try:
        response = await megaVoceroTranslator.run()
        clean_up_tmp_folder()
        return response
    except Exception as ex:
        logging.info(f"Error occured: \n{ex}")
        clean_up_tmp_folder()
        return func.HttpResponse(
                "This HTTP triggered ended with error.",
                status_code=500
        )

And the full Exception im getting is:

Exception while executing function: Functions.MegaVoceroTranslator Result: Failure
Exception: TypeError: <azure.functions.decorators.function_app.FunctionBuilder object at 0x7f5015b52110> is not a module, class, method, or function.
Stack:   File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 485, in _handle__function_load_request
    self._functions.add_function(
  File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/functions.py", line 366, in add_function
    annotations = typing.get_type_hints(func)
  File "/usr/local/lib/python3.10/typing.py", line 1856, in get_type_hints
    raise TypeError('{!r} is not a module, class, method, '

All the examples i can find have the same structure i cant find where the problem is

I have tried changin the file nime to function_app, and more decoreators but nothig worked


Solution

  • I got the same error when I tried with your code.

    azure-functions==1.20.0
    Python 3.11.9
    

    This error occurs if the function attempts to use the azure.functions.decorators module, which is part of Azure Functions v2.x and later, in a Python v1 environment.

    To resolve this error, I have updated the function code of init.py as below:

    init.py:

    import logging
    import shutil
    
    import azure.functions as func
    
    import MegaVoceroTranslator.megaVoceroTranslator as megaVoceroTranslator
    
    
    def clean_up_tmp_folder():
        tmp_path = 'tmp'
        try:
            shutil.rmtree(tmp_path)
            logging.info(f"Successfully deleted {tmp_path}")
        except FileNotFoundError:
            logging.info(f"{tmp_path} does not exist")
        except Exception as e:
            logging.error(f"Error deleting {tmp_path}: {str(e)}")
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
    
        try:
            response_message = megaVoceroTranslator.run()
            clean_up_tmp_folder()
            return func.HttpResponse(
                response_message,
                status_code=200
            )
        except Exception as ex:
            logging.error(f"Error occurred: \n{ex}")
            clean_up_tmp_folder()
            return func.HttpResponse(
                "This HTTP triggered function ended with an error.",
                status_code=500
            )
    

    megaVoceroTranslator.py:

    # MegaVoceroTranslator/megaVoceroTranslator.py
    def run():
    
        return "Hello from MegaVoceroTranslator!"
    

    Console output:

    (.venv) C:\Users\uname\MegaVoceroTranslator>func start
    Found Python version 3.11.9 (py).
    
    Azure Functions Core Tools
    Core Tools Version:       4.0.5907 Commit hash: N/A +807e89766a92b14fd07b9f0bc2bea1d8777ab209 (64-bit)       
    Function Runtime Version: 4.834.3.22875
    
    [2024-08-07T08:25:00.643Z] Worker process started and initialized.
    
    Functions:
    
            MegaVoceroTranslator: [GET,POST] http://localhost:7071/api/MegaVoceroTranslator
    
    For detailed output, run func with --verbose flag.
    [2024-08-07T08:25:05.505Z] Executing 'Functions.MegaVoceroTranslator' (Reason='This function was programmatically called via the host APIs.', Id=f212ad0d-fe36-4221-87a7-dc585d113f6b)
    [2024-08-07T08:25:05.654Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.       
    [2024-08-07T08:25:05.656Z] Python HTTP trigger function processed a request.
    [2024-08-07T08:25:05.656Z] Successfully deleted tmp
    [2024-08-07T08:25:05.765Z] Executed 'Functions.MegaVoceroTranslator' (Succeeded, Id=f212ad0d-fe36-XXX7a7-dc585d113f6b, Duration=333ms)
    [2024-08-07T08:25:08.798Z] Worker process started and initialized.
    

    Function response after deleting tmp folder:

    enter image description here

    enter image description here