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

使用Python脚本为OCI计算实例添加freeform_tag时遇TypeError错误

问题解决:OCI Python脚本添加自由格式标签时的TypeError错误

错误原因分析

错误信息:

update_volume_response = core_client.update_instance(instance_id=i.id, update_instance_details = oci.core.models.UpdateInstanceDetails(freeform_tags))
TypeError: UpdateInstanceDetails.init() takes 1 positional argument but 2 were given

出现该错误有三个核心问题:

  1. 参数传递方式错误:UpdateInstanceDetails构造函数需使用关键字参数,不能直接将字典作为位置参数传入
  2. 拼写错误:代码里的freefrom_tag是笔误,正确参数名为freeform_tags
  3. 缺少必填参数:调用update_instance时漏传了instance_id=i.id

修正后的代码

core_client = oci.core.compute_client.ComputeClient(config)
core_client.base_client.set_region('us-phoenix-1')
vol=oci.core.BlockstorageClient(config)

# 列出指定 compartment 下已停止的实例
lista = core_client.list_instances(compartment_id=compartment_id,lifecycle_state="STOPPED")
for i in lista.data:
    # 正确构造更新实例的详情对象
    update_details = oci.core.models.UpdateInstanceDetails(
        freeform_tags={"shutdown": "no"}
    )
    # 调用更新接口,传入实例ID和更新详情
    update_response = core_client.update_instance(
        instance_id=i.id,
        update_instance_details=update_details
    )
    print(update_response)
    break

关键修正点说明

  • 规范实例化对象:用关键字参数freeform_tags传递标签字典,而非直接传位置参数
  • 修正拼写错误:将freefrom_tag改为正确的freeform_tags
  • 补充必填参数:update_instance必须指定要更新的实例ID(即循环中的i.id)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 17:55:25