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

Azure函数调用本地Python模块后部署异常,无HTTP触发器显示

Azure函数调用本地Python模块后部署异常,无HTTP触发器显示

我在Python 3.12.3应用里有如下代码结构:

**ROOT**
-- .venv
-- azure_functions
---- function_app.py
---- host.json
---- local.settings.json
---- requirements.json
-- src
---- __init__.py
---- main.py
-- tests
---- test_action.py
-- .gitignore
-- requirements.txt
-- setup.py

各目录/文件说明:

  • .venv:虚拟环境目录,已安装根目录和azure_functions文件夹下requirements.json的依赖
  • azure_functions:存放Azure函数相关文件(HTTP触发器)
  • function_app.py:有问题的HTTP触发器,无法调用main.py里的llm_call函数
  • azure_functions/requirements.json:仅包含一行azure-functions
  • src/__init__.py(空文件)和src/main.py:模块及定义,main.py里的方法(比如llm_call)可以从tests文件夹正常调用,但无法从azure_functions目录调用
  • requirements.txt:main.py相关依赖
  • tests:测试文件夹,test_action.py只需简单配置就能调用main.py的方法:
    from src.main import llm_call
    
  • setup.py:简单的包/模块配置:
    # a simple package/module related configuration
    from setuptools import setup, find_packages
    
    setup(name="main", version="1.0", packages=find_packages())
    

问题现象

当我在function_app.py中通过以下代码引入main包的方法时:

from src.main import llm_call # just with this single new line, no actual usage of this below

VS Code里没有报错,但部署到Azure后,部署看似失败(日志里只有一行AM appname-func: No HTTP triggers found.),并且Azure资源UI(Function App => Functions)里的函数消失,无法在Azure UI中看到。

一旦移除这行导入代码并重新部署,所有逻辑恢复正常,函数会重新出现在上述界面中。看起来问题出在function_app.py调用main.py模块的方式上,但我不知道如何排查。

Update

main.py代码:

from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
    ChatPromptTemplate
)
from typing import Callable
from langchain.chains import LLMChain
from langchain.schema import BaseOutputParser
import os

model_to_use="gpt-4o-mini"

class CustomOutputParser(BaseOutputParser):
    def parse(self, text: str):
        return text

def llm_call(model, system_template, human_template, param_provider: Callable[[], dict], custom_output_parser) -> str:
    try:
        api_key = os.environ['OPENAI_API_KEY']
        temperature=0.7
        max_tokens=2048
        chat_model = ChatOpenAI(model=model, api_key=api_key, temperature=temperature, max_tokens=max_tokens)
        system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)

        human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
        chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

        chain = LLMChain(
            llm=chat_model,
            prompt=chat_prompt,
            output_parser=custom_output_parser
        )

        params = param_provider()
        return chain.run(**params)
    except Exception as e:
        print(f"An error occurred while calling the LLM: {e}")
        raise e

函数代码(function_app.py):

import azure.functions as func
import logging
from src.main import llm_call

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

@app.route(route="llm_httptrigger")
def llmhttptrigger(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request!')

测试代码(test_action.py):

import unittest
from src.main import llm_call

class TestCallLLMFunction(unittest.TestCase):
    def test_call_llm(self):
        category = "category1"
        ..

Update 2

感谢@PravallikaKV建议将函数相关代码移到根目录,但这没有解决问题。我仍然无法在门户上看到函数,部署输出里仍然显示:

5:26:37 PM appname-func: No HTTP triggers found.

说明一下,这个结构是VS Code向导生成的,我选择了azure_functions目录作为函数代码的存放位置。如果移除对main.py函数的调用,它可以正常工作,我能通过Web API请求调用这个“hello world”风格的HTTP触发器,也就是这种情况下部署是成功的。

补充说明:我用以下步骤创建venv文件夹:

1. python -m venv .venv  
2. .venv\Scripts\activate
2.1. 在VS Code中配置.venv的Python解释器路径
3. pip.exe install -r .\requirements.txt
4. pip.exe install -r .\azure_functions\requirements.txt
5. pip install -e .

我感觉Azure函数端看不到某些venv配置,但因为部署过程中没有触发错误,我不知道该如何继续排查。

Update 3

如果从头创建Azure函数,向导的其中一步是配置虚拟环境:
在配置过程中出现错误,提示我使用的Python 3.12.3版本不被支持,即使提供.venv文件夹中Python的绝对路径也会出现同样错误。

之前我跳过了这部分,现在尝试配置时看到上述错误。

Update 4

显然,我之前使用的Python 3.12版本不被Azure支持,但切换到3.11版本解决了Update 3中的问题,但仍然没有解决实际的触发器丢失问题。

备注:内容来源于stack exchange,提问作者dododo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 16:09:52