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

使用Azure异步Python SDK遍历VM列表时触发AttributeError

Azure异步API获取虚拟机列表报错问题

我编写了一个简单的异步Python程序,用于获取Azure订阅中的所有Virtual Machines列表,但在遍历VM列表时出现错误,而遍历Web应用列表则无异常。

完整程序代码

from azure.identity.aio import AzureCliCredential
from azure.mgmt.compute.aio import ComputeManagementClient
from azure.mgmt.network.aio import NetworkManagementClient
from azure.mgmt.web.aio import WebSiteManagementClient
from azure.mgmt.sql.aio import SqlManagementClient
import asyncio

credential = AzureCliCredential()

compute_client = ComputeManagementClient(credential, "XXXXX")
web_client = WebSiteManagementClient(credential, "XXXX")

async def get_as_list():
    as_list = web_client.web_apps.list()
    async for _as in as_list:
        pass
    return as_list
async def get_vm_list():
    vm_list = compute_client.virtual_machines.list_all()
    async for vm in vm_list:
        pass
    return vm_list

async def main():
    await get_as_list()
    await get_vm_list()

if __name__ == '__main__':
    asyncio.run(main())

报错信息

Traceback (most recent call last):
  File "c:\Users\Kristian-Laptop\Desktop\Work\Azure Inventory\Inventory with UI\test.py", line 32, in <module>
    asyncio.run(main())
  File "C:\Users\Kristian-Laptop\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\Kristian-Laptop\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 647, in run_until_complete
    return future.result()
  File "c:\Users\Kristian-Laptop\Desktop\Work\Azure Inventory\Inventory with UI\test.py", line 28, in main
    await get_vm_list()
  File "c:\Users\Kristian-Laptop\Desktop\Work\Azure Inventory\Inventory with UI\test.py", line 22, in get_vm_list
    async for vm in vm_list:
  File "C:\Users\Kristian-Laptop\.virtualenvs\Inventory_with_UI-ioU1jGkq\lib\site-packages\azure\core\async_paging.py", line 163, in __anext__   
    return await self.__anext__()
  File "C:\Users\Kristian-Laptop\.virtualenvs\Inventory_with_UI-ioU1jGkq\lib\site-packages\azure\core\async_paging.py", line 157, in __anext__   
    self._page = await self._page_iterator.__anext__()
  File "C:\Users\Kristian-Laptop\.virtualenvs\Inventory_with_UI-ioU1jGkq\lib\site-packages\azure\core\async_paging.py", line 99, in __anext__    
    self._response = await self._get_next(self.continuation_token)
  File "C:\Users\Kristian-Laptop\.virtualenvs\Inventory_with_UI-ioU1jGkq\lib\site-packages\azure\mgmt\compute\v2022_08_01\aio\operations\_virtual_machines_operations.py", line 1486, in get_next
    request = prepare_request(next_link)
  File "C:\Users\Kristian-Laptop\.virtualenvs\Inventory_with_UI-ioU1jGkq\lib\site-packages\azure\mgmt\compute\v2022_08_01\aio\operations\_virtual_machines_operations.py", line 1471, in prepare_request
    _next_request_params["api-version"] = self._config.api_version
AttributeError: 'ComputeManagementClientConfiguration' object has no attribute 'api_version'

解决方案

这个错误源于Azure Compute Management异步客户端的版本兼容问题,可通过以下步骤解决:

  • 更新Azure Compute管理包到最新稳定版
    执行命令更新依赖:

    pip install --upgrade azure-mgmt-compute
    
  • 显式指定API版本初始化客户端
    创建ComputeManagementClient时手动指定稳定API版本(如2023-07-01),规避默认版本的配置缺陷:

    compute_client = ComputeManagementClient(credential, "XXXXX", api_version="2023-07-01")
    
  • 统一更新所有Azure异步SDK包
    确保所有相关Azure SDK包版本一致,避免跨版本兼容问题:

    pip install --upgrade azure-identity azure-mgmt-compute azure-mgmt-web azure-mgmt-network azure-mgmt-sql
    

Web应用列表遍历正常,是因为WebSiteManagementClient的SDK实现不存在该版本配置问题,默认API版本可正常调用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 06:35:28