Install & Compatibility
Where this runs
tested against v4.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.570s · 28.5MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 2.7s · import 0.488s · 29MB
27MB installed
● package 27MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
IotHubClient
✓ from azure.mgmt.iothub import IotHubClient
✗ from azure.mgmt.iothub.iot_hub_client import IotHubClient
The client class is directly exposed under the top-level package for simplified access.
DefaultAzureCredential
✓ from azure.identity import DefaultAzureCredential
✗ from azure.common.credentials import ServicePrincipalCredentials
Older authentication methods like ServicePrincipalCredentials (from azure-common/msrestazure) have been replaced by the unified azure-identity package in v4.x and later.
This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential` and list all IoT Hubs within your specified Azure subscription. Ensure your `AZURE_SUBSCRIPTION_ID` environment variable is set. For local development, `DefaultAzureCredential` will also pick up credentials from `az login` (Azure CLI).
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.iothub import IotHubClient
# Ensure AZURE_SUBSCRIPTION_ID is set in your environment variables
# For local development, also set AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET
# or use 'az login' for Azure CLI credential.
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'your-subscription-id') # Replace with your actual subscription ID or ensure env var is set
if not subscription_id or subscription_id == 'your-subscription-id':
raise ValueError("Please set the AZURE_SUBSCRIPTION_ID environment variable or replace 'your-subscription-id' in the code.")
# Acquire a credential object using DefaultAzureCredential
# This attempts various authentication methods (environment variables, managed identity, CLI, etc.)
credential = DefaultAzureCredential()
# Construct an IoT Hub client
client = IotHubClient(credential, subscription_id)
# Example: List all IoT Hubs in the subscription
print("Listing all IoT Hubs in the subscription...")
iot_hubs = client.iot_hub_resource.list_by_subscription()
found_hubs = False
for hub in iot_hubs:
found_hubs = True
print(f" - Name: {hub.name}, Location: {hub.location}")
if not found_hubs:
print("No IoT Hubs found in this subscription.")
# Example: Get an IoT Hub (replace with actual resource group and hub name if you have one)
# resource_group_name = os.environ.get('AZURE_RESOURCE_GROUP', 'my-resource-group')
# hub_name = os.environ.get('AZURE_IOT_HUB_NAME', 'my-iothub')
# try:
# single_hub = client.iot_hub_resource.get(resource_group_name, hub_name)
# print(f"\nRetrieved single IoT Hub: {single_hub.name} in {single_hub.location}")
# except Exception as e:
# print(f"\nCould not retrieve IoT Hub {hub_name}: {e}")
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'azure.mgmt.iothub.iot_hub_client'
The import path for `IotHubClient` has been refactored in newer versions of the `azure-mgmt-iothub` library.
fixImport `IotHubClient` directly from `azure.mgmt.iothub` instead of its submodule.
Authentication failed for this device, renew token or certificate and reconnect
This error, often associated with `401003 IoTHubUnauthorized`, indicates issues with the credentials (SAS token, certificate) used for authentication, such as expiration or incorrect configuration, or insufficient Azure RBAC permissions.
fixEnsure that the Azure Active Directory environment variables (`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`, `AZURE_SUBSCRIPTION_ID`) are correctly set up and that the service principal or user has the necessary RBAC permissions (e.g., 'IoT Hub Data Contributor' or 'Owner') on the IoT Hub. Use `DefaultAzureCredential` for authentication.
Resource 'IOTHUBADEM' was disallowed by Azure: This policy maintains a set of best available regions where your subscription can deploy resources...
This error occurs when attempting to create an IoT Hub in an Azure region that is restricted by an Azure Policy, often encountered with free or student subscriptions.
fixSelect an allowed region for resource deployment. You can check the 'Allowed Locations' policy in your Azure subscription's Policy assignments to find permitted regions.
AttributeError: 'IotHubResourceOperations' object has no attribute 'config'
This `AttributeError` indicates an attempt to access a `config` attribute that no longer exists directly on operation groups (like `IotHubResourceOperations`) in newer versions of the Azure SDK for Python, due to internal refactoring.
fixIn modern Azure SDKs, client configuration is typically passed during client instantiation (e.g., `IotHubClient(credential=..., subscription_id=..., **kwargs)`) or managed internally by the client. Avoid direct access to a `config` attribute on operation objects. If using an older code snippet, refer to the current SDK documentation for the correct way to pass or access configuration parameters.
Upgrade
Version history
4.0.0latest on PyPI · released Apr 9, 2025
Audit
Dependencies
azure-corerequiredProvides shared primitives, exceptions, and operations for the Azure SDK. Required for HTTP pipeline and error handling.
azure-identityrequiredProvides Azure Active Directory authentication support for Azure SDK clients. Essential for authenticating with Azure services.