Azure Machine Learning SDK (v1)

1.62.0 · deprecated · verified Wed Apr 15

The Azure Machine Learning SDK v1 (meta-package for `azureml-core`) is used to build and run machine learning workflows upon the Azure Machine Learning service. It enables managing cloud resources, training models, and deploying them as web services. As of March 31, 2025, SDK v1 has been deprecated, with support ending on June 30, 2026. Users are strongly advised to migrate to Azure Machine Learning Python SDK v2 (`azure-ai-ml`) for continued support and new features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to an Azure ML Workspace, define a custom environment, create a compute target (or use 'local'), and submit a simple Python script as an experiment using `ScriptRunConfig` in Azure ML SDK v1. A `config.json` file in a `.azureml` subdirectory is the recommended way to connect to a workspace.

import os
from azureml.core import Workspace, Experiment, Environment, ScriptRunConfig
from azureml.core.compute import ComputeTarget, AmlCompute
from azureml.core.compute_target import ComputeTargetException
from azureml.core.conda_dependencies import CondaDependencies

# Create a dummy script file
with open('train_script.py', 'w') as f:
    f.write("""
import argparse
import os
import time
print("Hello from Azure ML v1 training script!")
parser = argparse.ArgumentParser()
parser.add_argument('--arg1', type=str, default='default_value')
args = parser.parse_args()
print(f"Argument 1: {args.arg1}")
time.sleep(5) # Simulate work
print("Script finished.")
""")

# Create a dummy conda environment file
with open('conda_env.yml', 'w') as f:
    f.write("""
name: my_env
dependencies:
  - python=3.8
  - pip:
    - azureml-defaults
""")

# NOTE: For a real scenario, replace placeholder values and ensure a config.json is available
# Authenticate and connect to your workspace
try:
    ws = Workspace.from_config(path='./.azureml', _file_name='config.json') # Reads from a local config.json
    print(f"Connected to workspace {ws.name}")
except Exception as e:
    print(f"Could not load workspace from config. Ensure .azureml/config.json exists or provide details manually: {e}")
    # Fallback to manual connection (replace with your actual 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_WORKSPACE_NAME', 'YOUR_WORKSPACE_NAME')
    ws = Workspace(subscription_id, resource_group, workspace_name)
    print(f"Connected to workspace {ws.name} via manual details.")

experiment_name = "my-first-v1-experiment"
experiment = Experiment(workspace=ws, name=experiment_name)

# Choose a name for your CPU cluster (or use 'local')
compute_name = "cpu-cluster"
compute_target = None

try:
    compute_target = ComputeTarget(workspace=ws, name=compute_name)
    print(f"Found existing compute target: {compute_name}")
except ComputeTargetException:
    print(f"Creating a new compute target: {compute_name}")
    compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_DS1_V2', max_nodes=1)
    compute_target = ComputeTarget.create(ws, compute_name, compute_config)
    compute_target.wait_for_completion(show_output=True)

# Define the environment
env = Environment.from_conda_specification(name='my-custom-env', file_path='conda_env.yml')

# Create a ScriptRunConfig
src = ScriptRunConfig(
    source_directory='.',
    script='train_script.py',
    compute_target=compute_target,
    environment=env,
    arguments=['--arg1', 'hello_from_run']
)

# Submit the run
run = experiment.submit(src)
print(f"Submitted run: {run.get_portal_url()}")
run.wait_for_completion(show_output=True)
print(f"Run completed with status: {run.status}")

view raw JSON →