使用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
出现该错误有三个核心问题:
- 参数传递方式错误:
UpdateInstanceDetails构造函数需使用关键字参数,不能直接将字典作为位置参数传入 - 拼写错误:代码里的
freefrom_tag是笔误,正确参数名为freeform_tags - 缺少必填参数:调用
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
相关产品推荐
相关产品推荐

