Azure Batch Management Client Library

19.0.0 · active · verified Thu Apr 09

The Azure Batch Management Client Library for Python, currently at version 19.0.0, allows developers to programmatically manage Azure Batch accounts, pools, and jobs. It provides interfaces for creating, updating, deleting, and querying Batch resources within an Azure subscription. Releases are frequent, typically aligned with new Azure Batch service API versions and broader Azure SDK updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Azure and list existing Azure Batch accounts within a subscription. It uses `DefaultAzureCredential` for flexible authentication and `BatchManagementClient` to interact with the Azure Batch management plane.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.batch import BatchManagementClient

# Ensure you have your Azure Subscription ID set as an environment variable (AZURE_SUBSCRIPTION_ID)
# or replace 'YOUR_SUBSCRIPTION_ID' with your actual subscription ID.
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "YOUR_SUBSCRIPTION_ID")

if subscription_id == "YOUR_SUBSCRIPTION_ID":
    print("WARNING: AZURE_SUBSCRIPTION_ID environment variable not set. Using placeholder.")
    print("Please set AZURE_SUBSCRIPTION_ID for actual resource management.")

try:
    # Acquire a credential object for authentication
    credential = DefaultAzureCredential()

    # Create a BatchManagementClient instance
    batch_client = BatchManagementClient(credential, subscription_id)

    print(f"Listing Batch accounts in subscription: {subscription_id}")
    
    # List all Batch accounts in the current subscription
    # This operation is synchronous.
    batch_accounts = batch_client.batch_account.list_by_subscription()

    found_accounts = 0
    for account in batch_accounts:
        print(f"- Account name: {account.name}, Location: {account.location}, Provisioning State: {account.provisioning_state.value}")
        found_accounts += 1
    
    if found_accounts == 0:
        print("No Batch accounts found in this subscription.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure you have set AZURE_SUBSCRIPTION_ID and are authenticated (e.g., via Azure CLI 'az login' or environment variables for a Service Principal).")

view raw JSON →