python中的forloop中存在name未定义错误



我得到的名称没有在python块中定义,尽管我已经正确地定义了它

我试过的:

accounts = ['abcd','abcde']
for account in accounts:
print(account)
new_col = 0
terminated_col = 0

try:
ak = jout['data']['client_id']
sk = jout['data']['client_secret']
credential = ServicePrincipalCredentials(tenant=tenant_var, client_id=ak, secret=sk)
subscription_client = SubscriptionClient(credential)
subscription = next(subscription_client.subscriptions.list())
resource_client = ResourceManagementClient(credential, subscription.subscription_id)
time.sleep(10)
except Exception as e:
print(e)  


for rg in resource_client.resource_groups.list():    
try:
print(rg.name) 

except Exception as e:
print(e)

获得错误,如:

for rg in resource_client.resource_groups.list():
NameError: name 'resource_client' is not defined

事实上,为列表中的所有帐户检查这些,所以理想情况下上面是正确的吗?有人能推荐吗

这个错误似乎发生在您定义它之前。我建议在try和exception语句中都定义它。基本上:

except Exception as e:
resource_client = ResourceManagementClient(credential, subscription.subscription_id)
print(e) 

resource_client仅在try的范围内可用。就在尝试之前,添加一个resource_client = None来声明它。

相关内容

  • 没有找到相关文章

最新更新