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

如何使用Python创建Azure订阅及解决SubscriptionCreationParameters属性报错

你遇到的报错是因为安装的azure-mgmt-subscription包版本和你查阅的0.5.0版本官方文档不匹配。SubscriptionCreationParameters是0.5.0及更早版本才有的类,2021年之后发布的3.x及以上版本的SDK已经重构了订阅创建相关的模型类,旧类被移除,所以会触发属性不存在的报错。


解决方法1:降级SDK版本适配现有代码

直接安装和文档匹配的0.5.0版本包,无需修改现有代码逻辑:

pip install azure-mgmt-subscription==0.5.0

注意:0.5.0是已停止维护的旧版本,不建议在生产环境长期使用。
另外你现有代码还有一处变量名错误:定义函数时传入参数是client_name,但调用时写的是未定义的变量client_sub,修复后才能正常运行。


解决方法2:适配最新版本SDK

如果使用新版本SDK(≥3.0.0),需要调整参数类的调用:
针对企业协议enrollment账户创建订阅的场景,旧的SubscriptionCreationParameters替换为EnrollmentAccountSubscriptionCreationParameters,修改后的代码示例如下:

import azure.mgmt.billing
import azure.mgmt.subscription
from azure.identity import AzureCliCredential

creds = AzureCliCredential()
enrollment_account_name = 'test' # 替换为你的enrollment账户名

def create_subscription(creds, enrollment_account_name):
    subscription_client = azure.mgmt.subscription.SubscriptionClient(creds)
    # 新版本使用对应场景的参数类
    creation_parameters = azure.mgmt.subscription.models.EnrollmentAccountSubscriptionCreationParameters(
        offer_type = 'MS-AZR-0003P',
        display_name = '你的订阅名称' # 新版本必填参数
    )
    creation_result = subscription_client.subscription_factory.begin_create_subscription_in_enrollment_account(
        enrollment_account_name,
        creation_parameters
    ).result() # 新版本创建是异步操作,需要调用result()等待完成
    
    return creation_result

output = create_subscription(creds, enrollment_account_name)
print(output)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 19:36:02