You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python版Azure Function启动报错:FunctionBuilder类型错误求助

Azure Function TypeError排查:FunctionBuilder对象不是模块/类/方法/函数

使用Python 3.10.5、azure-functions==1.20.0开发Azure Function,本地运行及部署到Azure时均触发TypeError,提示<azure.functions.decorators.function_app.FunctionBuilder object at 0x7f5015b52110>不是模块、类、方法或函数。

问题代码(init.py)

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
        )

完整异常栈

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, '

排查与解决方案

  • 版本兼容性不匹配:azure-functions==1.20.0中的func.FunctionApp()属于V2模型装饰器,而当前使用的Azure Functions Runtime或Python Worker版本无法正确解析该装饰器生成的FunctionBuilder对象。
    • 解决方法:改用V1模型的装饰器写法(兼容旧版Runtime和包版本),替换原有代码的装饰器部分:
      # 移除app = func.FunctionApp()及相关装饰器,替换为:
      async def main(req: func.HttpRequest) -> func.HttpResponse:
          # 原有main函数逻辑不变
      
      # 用传统装饰器绑定触发器和函数名
      main = func.HttpTrigger(route="MegaVoceroTranslator")(main)
      main = func.FunctionName("MegaVoceroTranslator")(main)
      
  • 装饰器API解析问题:若坚持使用V2模型,需确保Azure Functions Runtime(本地CLI和云端)与azure-functions包版本兼容。升级本地func CLI到最新稳定版(运行func --version检查,若版本低于4.x建议升级),同时云端Runtime也需配置为对应兼容版本。
  • 检查依赖安装:确认本地虚拟环境中azure-functions版本正确,无依赖冲突。可重新安装依赖:pip install --force-reinstall azure-functions==1.20.0。

内容的提问来源于stack exchange,提问作者Ian Szejer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 02:55:08