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

部署至Azure Functions App后函数未被识别的问题求助

问题描述

将Python编写的Azure Functions App部署成功后,平台未识别到函数。本地调试时出现「未找到作业函数」「无HTTP路由映射」错误,通过GitHub Actions部署至Azure门户后也看不到对应函数。


相关信息

主函数代码(__init__.py)

import logging
import os
import azure.functions as func
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from .feature_extraction import extract_section_headers


# Set up logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)


@app.route(route="HttpTrigger", auth_level=func.AuthLevel.ANONYMOUS)
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    document_url = req.params.get('documentUrl')
    if not document_url:
        try:
            req_body = req.get_json()
            document_url = req_body.get('documentUrl')
        except ValueError:
            pass

    if not document_url:
        return func.HttpResponse(
             "Please pass a document URL in the query string or in the "
             "request body",
             status_code=400
        )

    # Azure credentials from environment variables
    endpoint = os.environ["DOCUMENT_INTELLIGENCE_ENDPOINT"]
    key = os.environ["DOCUMENT_INTELLIGENCE_KEY"]

    credential = AzureKeyCredential(key)
    document_intelligence_client = DocumentIntelligenceClient(
        endpoint=endpoint,
        credential=credential
    )

    try:
        poller = document_intelligence_client.begin_analyze_document_from_url(
            "prebuilt-layout", document_url)
        result = poller.result()

        # Extract section headers
        section_headers = extract_section_headers(result)
        logger.info(section_headers)

        return func.HttpResponse(
            body=str(section_headers),
            mimetype="application/json",
            status_code=200
        )
    except Exception as e:
        logger.error(f"Failed to analyze document. Exception: {str(e)}")
        return func.HttpResponse(
            f"Failed to analyze document. Exception: {str(e)}",
            status_code=500
        )


app.register_functions(extract_section_headers)

feature_extraction.py代码片段

from typing import List
import azure.functions as func

feature_extraction = func.Blueprint()

@feature_extraction.function_name("extract_section_headers")
def extract_section_headers(analyze_result) -> List[dict]:
"""
Extracts section headers from the analyzed document.
"""
# 注:原代码片段不完整,假设后续有具体实现

项目结构

函数项目根目录包含:

  • HttpTrigger文件夹(内含__init__.py和feature_extraction.py)
  • local.settings.json
  • requirements.txt等标准Functions项目文件

local.settings.json配置

{
    "IsEncrypted": false,
    "Values": {
      "FUNCTIONS_WORKER_RUNTIME": "python",
      "DOCUMENT_INTELLIGENCE_ENDPOINT": "https://smartformprocessor.cognitiveservices.azure.com/",
      "DOCUMENT_INTELLIGENCE_KEY": "861e665469694f99acc152609d367628",
      "AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
    }
}

本地调试错误日志

[2024-02-12T08:51:10.900Z] Reading functions metadata (Host)
[2024-02-12T08:51:10.900Z] 0 functions found (Host)
[2024-02-12T08:51:10.901Z] Reading functions metadata (Custom)
[2024-02-12T08:51:10.901Z] 1 functions found (Custom)
[2024-02-12T08:51:10.901Z] 0 functions loaded
[2024-02-12T08:51:10.901Z] Generating 0 job function(s)
[2024-02-12T08:51:10.904Z] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
[2024-02-12T08:51:10.904Z] HttpOptions
[2024-02-12T08:51:10.904Z] Initializing function HTTP routes
[2024-02-12T08:51:10.904Z] No HTTP routes mapped

排查与解决方法

1. 修复Blueprint注册错误

主函数代码中错误地注册了普通函数,而非Blueprint实例。修改__init__.py:

  • 调整导入语句:
    # 原代码:from .feature_extraction import extract_section_headers
    from .feature_extraction import feature_extraction, extract_section_headers
    
  • 修改注册语句:
    # 原代码:app.register_functions(extract_section_headers)
    app.register_functions(feature_extraction)
    

2. 修正Blueprint函数定义

extract_section_headers是工具函数,不需要作为独立Function部署,应移除Blueprint装饰器:

from typing import List

def extract_section_headers(analyze_result) -> List[dict]:
    """
    Extracts section headers from the analyzed document.
    """
    # 补充具体实现代码

此时可直接删除app.register_functions语句,无需注册工具函数。

3. 验证项目结构与依赖

  • 确保函数文件夹下的主文件为__init__.py,Azure Functions会扫描该文件识别函数。
  • 检查requirements.txt包含所有依赖:azure-functions、azure-ai-documentintelligence、azure-core等,本地调试前执行pip install -r requirements.txt安装依赖。

4. 检查部署流程

  • GitHub Actions部署脚本需包含依赖安装步骤:pip install -r requirements.txt。
  • 查看部署日志,排查是否存在依赖缺失、代码语法错误等导致函数加载失败的问题。

内容的提问来源于stack exchange,提问作者Tyler B McCarthy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 00:45:39