Azure Subscription Management Client Library for Python
The `azure-mgmt-subscription` library provides Python APIs for managing Azure subscriptions. It allows developers to programmatically interact with Azure to get subscription details, list available locations, manage subscription aliases, and perform other subscription-related operations. The current stable version is 3.1.1. The Azure SDK for Python generally follows an active release cadence for its management libraries, with updates addressing bug fixes, new features, and breaking changes across major versions.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes, including a new code generator. Operations that returned `msrest.polling.LROPoller` now return `azure.core.polling.LROPoller` and are prefixed with `begin_`. `CloudError` was removed and most exceptions are now `azure.core.exceptions.HttpResponseError`. It also introduced stable and official support for async programming.
- breaking Import paths for `SubscriptionClient` and `SubscriptionClientConfiguration` changed. They can no longer be imported from `azure.mgmt.subscription.subscription_client`. Model classes also shifted from sub-modules (e.g., `azure.mgmt.subscription.models.my_class`) to direct import from `azure.mgmt.subscription.models`.
- gotcha The `azure-mgmt-subscription` library (and other Azure SDK Python packages) officially ended support for Python 2.7 on January 1, 2022. Version 3.1.1 requires Python 3.7+. Newer management libraries often require Python 3.9+.
- gotcha When moving an Azure subscription to a different tenant, both the source and destination subscriptions must reside within the same Microsoft Entra (formerly Azure Active Directory) tenant. This is a crucial prerequisite for successful transfers.
- gotcha Azure resources created using the classic deployment model cannot be moved between subscriptions. All resources in the source subscription must be migrated to the Azure Resource Manager (ARM) model before a subscription transfer can occur.
- gotcha Version 3.1.0 had a bug where the `api_version` was missing for some operations (e.g., `list_locations`), causing exceptions. This was fixed in version 3.1.1.
Install
-
pip install azure-mgmt-subscription azure-identity
Imports
- SubscriptionClient
from azure.mgmt.subscription import SubscriptionClient
- Subscription
from azure.mgmt.subscription.models import Subscription
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.subscription import SubscriptionClient
# Ensure AZURE_SUBSCRIPTION_ID, AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET are set as environment variables
# for DefaultAzureCredential to work in non-interactive environments.
# For local development, Azure CLI or VS Code authentication might be used, removing the need for explicit env vars.
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')
if not subscription_id or subscription_id == 'YOUR_SUBSCRIPTION_ID':
raise ValueError("Please set the AZURE_SUBSCRIPTION_ID environment variable or replace 'YOUR_SUBSCRIPTION_ID'.")
try:
# Authenticate using DefaultAzureCredential
credential = DefaultAzureCredential()
# Create a SubscriptionClient
subscription_client = SubscriptionClient(credential)
# List all subscriptions accessible by the credential
print("Listing all subscriptions...")
for subscription in subscription_client.subscriptions.list():
print(f"Subscription ID: {subscription.subscription_id}, Display Name: {subscription.display_name}, State: {subscription.state}")
# Get details for a specific subscription
print(f"\nGetting details for subscription ID: {subscription_id}")
specific_subscription = subscription_client.subscriptions.get(subscription_id)
print(f"Display Name: {specific_subscription.display_name}, State: {specific_subscription.state}")
# List locations for the specific subscription
print(f"\nListing locations for subscription ID: {subscription_id}")
for location in subscription_client.subscriptions.list_locations(subscription_id):
print(f"Location: {location.display_name} ({location.name})")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure you are logged into Azure (e.g., via `az login`) or environment variables are set correctly.")