{"library":"azure-mgmt-compute","code":"import os\nfrom azure.identity import DefaultAzureCredential\nfrom azure.mgmt.compute import ComputeManagementClient\n\n# Set environment variables for authentication and subscription ID\n# AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET\n# AZURE_SUBSCRIPTION_ID\n\nsubscription_id = os.getenv(\"AZURE_SUBSCRIPTION_ID\", \"\")\nif not subscription_id:\n    print(\"Please set the AZURE_SUBSCRIPTION_ID environment variable.\")\n    exit(1)\n\n# Authenticate using DefaultAzureCredential (looks for env vars, managed identity, etc.)\ncredential = DefaultAzureCredential()\n\n# Create a Compute Management Client\ncompute_client = ComputeManagementClient(credential, subscription_id)\n\ntry:\n    # List all virtual machines in the subscription\n    print(\"Listing all virtual machines:\")\n    for vm in compute_client.virtual_machines.list_all():\n        print(f\"- VM Name: {vm.name}, Location: {vm.location}\")\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    print(\"Ensure you have set AZURE_SUBSCRIPTION_ID and have appropriate permissions.\")\n","lang":"python","description":"This quickstart initializes the `ComputeManagementClient` using `DefaultAzureCredential` for authentication, which is the recommended approach for Azure SDKs. It then proceeds to list all virtual machines accessible within the specified Azure subscription. Ensure `AZURE_SUBSCRIPTION_ID` and other necessary Azure Identity environment variables (e.g., `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`) are set for successful authentication.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}