Registry / azure / azure-mgmt-iothub

azure-mgmt-iothub

JSON →
library4.0.0pypypi✓ verified 25d ago

The `azure-mgmt-iothub` library is the Microsoft Azure IoT Hub Management Client Library for Python, enabling programmatic control and management of Azure IoT Hub resources. It allows for creating, updating, deleting, and querying IoT Hubs and their associated entities (like consumer groups and private endpoints). The current version is 4.0.0, and it follows the Azure SDK for Python's release cadence, with updates typically aligned with new Azure service features or platform changes.

pip install azure-mgmt-iothub
INSTALL
IMPORT
SIG · AZURE-MGMT-IOTHUB
A
azure-mgmt-iothub
azurepythonv4.0.0
Install
2.7s avg
Import
529ms
Disk
27MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.570s · 28.5MB
glibc
py 3.103.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}")
Debug
Known issues
breakingVersion 4.0.0 introduced significant breaking changes, migrating away from `msrestazure` based authentication and client construction. It now exclusively uses `azure-core` for HTTP operations and `azure-identity` for authentication.
fix
Update your authentication code to use `DefaultAzureCredential` or other credentials from the `azure-identity` package. The client constructor now typically takes `(credential, subscription_id)` directly, without an explicit 'base_url' parameter.
affects: >=4.0.0
gotchaListing operations (e.g., `list_by_subscription()`, `list_by_resource_group()`) return an iterator, not a complete list. You must iterate over the result to access individual resources. Attempting to index directly or treat it as a list will fail.
fix
Always iterate over the results of listing methods (e.g., `for item in client.iot_hub_resource.list_by_subscription(): ...`) to process them. If a full list is required, convert the iterator using `list()`: `all_hubs = list(client.iot_hub_resource.list_by_subscription())`.
affects: >=1.0.0
gotchaMany management operations (e.g., getting, creating, updating IoT Hubs) require a resource group name in addition to the subscription ID. Forgetting to provide this or providing an incorrect one will result in `ResourceNotFound` or `ValidationError` exceptions.
fix
Always refer to the method signature in the documentation for operations that might require a resource group name, and ensure it's provided as an argument where needed. For example, `client.iot_hub_resource.get(resource_group_name, hub_name)`.
affects: >=1.0.0
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.
fix
Import `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.
fix
Ensure 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.
fix
Select 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.
fix
In 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.
Agent activity
23 hits · last 30 days
node
20
OpenAI (training)
1
Resources
azure-mgmt-iothub — pip install azure-mgmt-iothub · libregistry