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

未使用VS Code扩展部署Azure Function后访问出现404错误求助

问题

我部署了一个执行简单计算的Azure Function,部署成功后显示“Your Functions 4.0 app is up and running”。代码如下:

import azure.functions as func
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.route(route="addition")
def main(req: func.HttpRequest) -> str:
    a = req.params.get("a")
    b = req.params.get("b")
    return "{}".format(int(a) * int(b))

访问/api/addition?a=2&b=3时出现404 Not Found错误,未得到预期结果。此前用VS Code Azure Function扩展部署能正常运行,但现在希望不通过VS Code完成部署,我的构建和部署管道任务如下:

构建管道任务

- task: UsePythonVersion@0
  inputs:
    versionSpec: $(pythonVersion)
    disableDownloadFromRegistry: true
    addToPath: true
    architecture: 'x64'
  displayName: 'Use python $(pythonVersion)'
- script: |
    python -m venv antenv
    source antenv/bin/activate
    pip install -r requirements.txt
  workingDirectory: $(projectRoot)
  displayName: 'Install requirements'

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(projectRoot)'
    includeRootFolder: false
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
    replaceExistingArchive: true

- publish: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
  artifact: drop

部署管道任务

- task: UsePythonVersion@0
  inputs:
    versionSpec: $(pythonVersion)
    disableDownloadFromRegistry: true
    addToPath: true
    architecture: 'x64'
    displayName: 'Use python $(pythonVersion)'
          
- task: AzureFunctionApp@2
  inputs:
    azureSubscription: $(azureSubscription)
    appType: 'functionAppLinux'
    appName: 'func-calculationtest'
    package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
    runtimeStack: 'PYTHON|3.10'
    deploymentMethod: 'auto'
解决方案

你遗漏了几个关键配置和检查步骤,以下是修复要点:

  1. 确认归档包包含完整的函数运行文件
    你的ArchiveFiles@2任务设置了includeRootFolder: false,需确保压缩包根目录包含Python V2函数的核心文件:

    • 函数代码文件(如__init__.py或function_app.py)
    • requirements.txt(必须包含azure-functions依赖)
      检查构建后的zip包内容,若文件结构错误,调整rootFolderOrFile参数,保证所有必要文件都在压缩包根目录。
  2. 添加Python V2专属应用配置
    Python V2函数需要手动添加以下应用程序设置(VS Code部署会自动配置,但管道部署需手动操作):
    在Azure门户的函数应用→配置→应用程序设置中添加/修改:

    • FUNCTIONS_EXTENSION_VERSION=~4
    • FUNCTIONS_WORKER_RUNTIME=python
    • FUNCTIONS_WORKER_RUNTIME_VERSION=3.10
    • AzureWebJobsFeatureFlags=EnableWorkerIndexing
  3. 验证运行时栈与部署配置匹配
    确保AzureFunctionApp@2任务的runtimeStack与函数应用的Python版本完全一致,同时确认函数应用运行模式:Python V2默认使用隔离进程模式,若本地使用非隔离模式,需添加配置FUNCTIONS_WORKER_RUNTIME_MODE=inprocess。

  4. 核对访问路径的正确性
    确认访问的完整路径为https://<你的函数应用名>.azurewebsites.net/api/addition?a=2&b=3,检查域名是否正确,以及函数应用是否修改过默认路由前缀/api。

  5. 查看日志定位加载错误
    在Azure门户的函数应用→监测→日志流中查看实时日志,确认函数是否被正确加载,是否存在依赖缺失、文件路径错误等初始化问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 06:40:59