The `azure-mgmt-compute` library is the Microsoft Azure Compute Management Client Library for Python, providing essential functionality for managing Azure compute resources like Virtual Machines, Virtual Machine Scale Sets, Disks, and Galleries. It enables developers to create, configure, manage, and scale Windows and Linux virtual machines programmatically. Currently at version 37.2.0, the library is actively maintained with frequent updates, often on a monthly or bi-monthly release cadence, reflecting ongoing development in the Azure platform.
pip install azure-mgmt-compute azure-identityVerified import paths — ran on the pinned version, not inferred.
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.
Remove explicit API version imports (e.g., `from azure.mgmt.compute.v20xx_xx_xx import ComputeManagementClient`). Instead, import `ComputeManagementClient` directly from `azure.mgmt.compute`. Import models directly from `azure.mgmt.compute.models`. If your application requires a specific non-latest API-Version, pin to an older package version.
Migrate to `azure-identity` library for authentication. Use `DefaultAzureCredential` or other credential types from `azure.identity` to create the client. Ensure `azure-identity` is installed: `pip install azure-identity`.
Update all calls to Azure Compute methods to pass query and header parameters as keyword arguments, not positional arguments.
Ensure `azure-common` is updated to a compatible version (e.g., `pip install --upgrade azure-common`) or install `azure-common==1.1.23` if you are using an older Python environment like Python 3.8 where specific issues were noted.
This often requires ensuring the client library and the Azure service API are in sync. If possible, upgrade `azure-mgmt-compute` to the latest version. If persistent, check the Azure status page or report to the Azure SDK team, providing the full error traceback and request details. Consider adding retry logic for transient errors.
Implement proper lifecycle management for Azure resources created programmatically. Use Azure Resource Tags, monitor resource usage with Azure Monitor, and regularly review your Azure spending to identify idle or orphaned resources. Leverage Azure Policy to enforce standards.
Set the `AZURE_SUBSCRIPTION_ID` environment variable in your environment (e.g., `export AZURE_SUBSCRIPTION_ID='<your-subscription-id>'` for Linux/macOS, or via system settings/CI environment variables). Ensure your subscription ID is a valid GUID.
Set the `AZURE_SUBSCRIPTION_ID` environment variable before running your application. You can obtain your subscription ID from the Azure portal or CLI. Example: `export AZURE_SUBSCRIPTION_ID='<your_subscription_id>'` (for Linux/macOS) or `set AZURE_SUBSCRIPTION_ID=<your_subscription_id>` (for Windows CMD).