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

队列触发型Azure Python函数用ManagedIdentityCredential报错:Azure CLI未找到

问题解决:Azure Python函数中ManagedIdentityCredential报Azure CLI未找到错误

问题描述

开发队列触发的Azure Python函数,触发后创建虚拟机,使用ManagedIdentityCredential()获取凭据时,出现以下错误:

Result: Failure Exception: CredentialUnavailableError: Azure CLI not found on path Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py"

引发错误的代码片段:

import base64
import azure.functions as func
import requests
from azure.identity import ManagedIdentityCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute import ComputeManagementClient

credential = ManagedIdentityCredential()

原因分析

  • ManagedIdentityCredential在本地开发环境中会依次尝试多个凭据源,其中包含Azure CLI。如果本地未安装Azure CLI,或者CLI未添加到系统PATH中,就会抛出该错误。
  • 在Azure函数运行时环境中,ManagedIdentityCredential本应自动使用函数应用的托管标识凭据,但本地开发时若未正确配置本地凭据源,就会触发CLI查找逻辑。

解决方案

本地开发场景

  1. 安装Azure CLI,并确保将其添加到系统PATH中
  2. 打开终端执行登录命令:az login,登录到目标Azure订阅
  3. (可选)改用DefaultAzureCredential,它会自动尝试多种本地凭据源(CLI、VS Code、环境变量等),同时适配本地开发和云端部署:
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()

Azure函数部署场景

  1. 给函数应用启用系统分配托管标识:
    • 进入Azure门户的函数应用资源页,找到「标识」选项卡,切换到「系统分配」标签,设置状态为「开启」后保存
  2. 为该托管标识分配创建VM所需的权限:
    • 找到目标资源组或订阅,进入「访问控制(IAM)」,添加角色分配,选择「虚拟机参与者」「网络参与者」「资源组参与者」等必要角色,将托管标识作为成员添加
  3. 部署函数后,ManagedIdentityCredential会自动获取托管标识的凭据,无需依赖Azure CLI,错误会自动消失

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 13:39:35