Azure Subscriptions Management Client
The `azure-mgmt-resource-subscriptions` library is the Microsoft Azure Resource Subscriptions Management Client Library for Python. It provides functionality to manage Azure subscriptions programmatically. As of the current release, 1.0.0b1, it is in a beta stage, indicating active development with potential for non-backward compatible changes. It is part of the broader Azure SDK for Python ecosystem, which typically follows a frequent release cadence for both stable and preview packages.
Common errors
-
ImportError: cannot import name 'SubscriptionClient' from 'azure.mgmt.resource'
cause The `SubscriptionClient` class has been moved from the `azure.mgmt.resource` package to its dedicated package `azure.mgmt.resource.subscriptions` as part of the Azure SDK's modularization efforts.fixChange the import statement to `from azure.mgmt.resource.subscriptions import SubscriptionClient` and ensure `pip install azure-mgmt-resource-subscriptions` has been run. -
azure.core.exceptions.ClientAuthenticationError: Authentication failed: AADTokenOperationError: AADTokenOperationError: Token acquisition failed. Correlation ID: ... Message: ManagedIdentityCredential authentication failed. See the troubleshooting guide for details. https://aka.ms/azsdk/python/identity/troubleshoot
cause `DefaultAzureCredential` or other credential types failed to authenticate, often due to missing or incorrect environment variables (e.g., `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`), misconfigured Managed Identity, or insufficient permissions.fixVerify that your environment variables for Azure authentication are correctly set, or that your Managed Identity has the necessary permissions. Refer to the Azure Identity troubleshooting guide for detailed debugging steps. -
azure.core.exceptions.ResourceNotFoundError: The subscription 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' could not be found. (SubscriptionNotFound)
cause The authenticated identity does not have access to the specified subscription ID, the subscription ID is incorrect, or the identity is authenticated to the wrong Azure Active Directory tenant. This is a common issue with Azure SDKs.fixEnsure the correct subscription ID is being used. Verify that the authenticated user or service principal has 'Reader' role (or higher) permissions on the target subscription. If working with multiple tenants, ensure the authentication context is set for the correct tenant. -
No registered resource provider found for location '{location}' and API version '{api-version}' for type '{resource-type}'.cause This error typically occurs when attempting to perform an operation (e.g., deploying a resource) for which the necessary Azure Resource Provider has not been registered in your subscription, or the specified location/API version is unsupported for that provider.fixRegister the required resource provider for your subscription using Azure CLI (`az provider register --namespace Microsoft.ResourceProviderName`) or PowerShell. Check the documentation for the specific resource type to confirm supported locations and API versions.
Warnings
- breaking This library is currently in a beta (preview) state (e.g., 1.0.0b1). Beta libraries are not recommended for production use as they may contain bugs or undergo non-backward compatible API changes without prior notice.
- breaking The `SubscriptionClient` and related subscription management functionalities were separated from the monolithic `azure-mgmt-resource` package into `azure-mgmt-resource-subscriptions`. Direct imports like `from azure.mgmt.resource import SubscriptionClient` will fail in newer SDK versions.
- gotcha When constructing a `SubscriptionClient`, an `api_version` parameter is available (defaulting to '2022-12-01'). Explicitly overriding this default to an unsupported or incorrect API version can lead to unexpected behavior or errors.
- gotcha The package requires Python 3.9+. Using it with older Python versions will result in installation or runtime errors.
Install
-
pip install azure-mgmt-resource-subscriptions azure-identity
Imports
- SubscriptionClient
from azure.mgmt.resource import SubscriptionClient
from azure.mgmt.resource.subscriptions import SubscriptionClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource.subscriptions import SubscriptionClient
# Ensure environment variables are set for authentication:
# AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
# (or other methods supported by DefaultAzureCredential)
credential = DefaultAzureCredential()
client = SubscriptionClient(credential=credential)
print("Listing Azure subscriptions:")
for subscription in client.subscriptions.list():
print(f" Name: {subscription.display_name} (ID: {subscription.subscription_id})")