Azure AI ML Client Library

1.32.0 · active · verified Fri Apr 10

The Microsoft Azure Machine Learning Client Library for Python (also known as the 'v2 SDK') is the primary Python interface for interacting with Azure Machine Learning. It enables developers to build, train, and deploy machine learning models, manage workspaces, compute resources, data, environments, and jobs programmatically. It's actively developed, with frequent releases, often on a monthly cadence, aligning with Azure service updates. The current version is 1.32.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to an Azure Machine Learning workspace using the `MLClient` and `DefaultAzureCredential`. It retrieves workspace details from environment variables (or placeholders) and then lists the custom environments registered in the workspace. Ensure `AZURE_SUBSCRIPTION_ID`, `AZURE_RESOURCE_GROUP`, and `AZURE_ML_WORKSPACE_NAME` environment variables are set, or replace placeholders with your actual values. Also, authenticate via Azure CLI (`az login`) or other methods for `DefaultAzureCredential` to work.

import os
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential

# Configure Azure credentials and workspace details
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')
resource_group = os.environ.get('AZURE_RESOURCE_GROUP', 'YOUR_RESOURCE_GROUP')
workspace_name = os.environ.get('AZURE_ML_WORKSPACE_NAME', 'YOUR_WORKSPACE_NAME')

# Instantiate DefaultAzureCredential
try:
    credential = DefaultAzureCredential()
    # Check if credential works by getting a token (optional, but good for early validation)
    _ = credential.get_token('https://management.azure.com/.default')
except Exception as e:
    print(f"Authentication failed: {e}")
    print("Please ensure you are logged in to Azure CLI, VS Code, or have appropriate environment variables set.")
    exit(1)

# Create an MLClient instance
ml_client = MLClient(
    credential=credential,
    subscription_id=subscription_id,
    resource_group_name=resource_group,
    workspace_name=workspace_name
)

print(f"Connected to Azure ML workspace: {ml_client.workspace_name}")

# Example: List existing environments in the workspace
print("Listing existing environments...")
environments = ml_client.environments.list()
for env in environments:
    print(f"- {env.name} (version: {env.version})")

view raw JSON →