| name | azure-data-tables-py |
| description | Azure Tables SDK for Python (Storage and Cosmos DB). Use for NoSQL key-value storage, entity CRUD, and batch operations.
Triggers: "table storage", "TableServiceClient", "TableClient", "entities", "PartitionKey", "RowKey".
|
| license | MIT |
| metadata | {"author":"Microsoft","version":"1.0.0","package":"azure-data-tables"} |
Azure Tables SDK for Python
NoSQL key-value store for structured data (Azure Storage Tables or Cosmos DB Table API).
Installation
pip install azure-data-tables azure-identity
Environment Variables
AZURE_STORAGE_ACCOUNT_URL=https://<account>.table.core.windows.net
COSMOS_TABLE_ENDPOINT=https://<account>.table.cosmos.azure.com
AZURE_TOKEN_CREDENTIALS=prod
Authentication & Lifecycle
🔑 Two rules apply to every code sample below:
- Prefer
DefaultAzureCredential. It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.
- Local dev:
DefaultAzureCredential works as-is.
- Production: set
AZURE_TOKEN_CREDENTIALS=prod (or AZURE_TOKEN_CREDENTIALS=<specific_credential>) to constrain the credential chain to production-safe credentials.
- Wrap every client in a context manager so HTTP transports, sockets, and token caches are released deterministically:
- Sync:
with <Client>(...) as client:
- Async:
async with <Client>(...) as client: and async with DefaultAzureCredential() as credential: (from azure.identity.aio)
Snippets may abbreviate this setup, but production code should always follow both rules.
import os
from azure.identity import DefaultAzureCredential, ManagedIdentityCredential
from azure.data.tables import TableServiceClient, TableClient
credential = DefaultAzureCredential(require_envvar=True)
endpoint = "https://<account>.table.core.windows.net"
with TableServiceClient(endpoint=endpoint, credential=credential) as service_client:
...
with TableClient(endpoint=endpoint, table_name="mytable", credential=credential) as table_client:
...
Client Types
| Client | Purpose |
|---|
TableServiceClient | Create/delete tables, list tables |
TableClient | Entity CRUD, queries |
Table Operations
service_client.create_table("mytable")
service_client.create_table_if_not_exists("mytable")
service_client.delete_table("mytable")
for table in service_client.list_tables():
print(table.name)
table_client = service_client.get_table_client("mytable")
Entity Operations
Important: Every entity requires PartitionKey and RowKey (together form unique ID).
Create Entity
entity = {
"PartitionKey": "sales",
"RowKey": "order-001",
"product": "Widget",
"quantity": 5,
"price": 9.99,
"shipped": False
}
table_client.create_entity(entity=entity)
table_client.upsert_entity(entity=entity)
Get Entity
entity = table_client.get_entity(
partition_key="sales",
row_key="order-001"
)
print(f"Product: {entity['product']}")
Update Entity
entity["quantity"] = 10
table_client.update_entity(entity=entity, mode="replace")
update = {
"PartitionKey": "sales",
"RowKey": "order-001",
"shipped": True
}
table_client.update_entity(entity=update, mode="merge")
Delete Entity
table_client.delete_entity(
partition_key="sales",
row_key="order-001"
)
Query Entities
Query Within Partition
entities = table_client.query_entities(
query_filter="PartitionKey eq 'sales'"
)
for entity in entities:
print(entity)
Query with Filters
entities = table_client.query_entities(
query_filter="PartitionKey eq 'sales' and quantity gt 3"
)
entities = table_client.query_entities(
query_filter="PartitionKey eq @pk and price lt @max_price",
parameters={"pk": "sales", "max_price": 50.0}
)
Select Specific Properties
entities = table_client.query_entities(
query_filter="PartitionKey eq 'sales'",
select=["RowKey", "product", "price"]
)
List All Entities
for entity in table_client.list_entities():
print(entity)
Batch Operations
from azure.data.tables import TableTransactionError
operations = [
("create", {"PartitionKey": "batch", "RowKey": "1", "data": "first"}),
("create", {"PartitionKey": "batch", "RowKey": "2", "data": "second"}),
("upsert", {"PartitionKey": "batch", "RowKey": "3", "data": "third"}),
]
try:
table_client.submit_transaction(operations)
except TableTransactionError as e:
print(f"Transaction failed: {e}")
Async Client
from azure.data.tables.aio import TableServiceClient, TableClient
from azure.identity.aio import DefaultAzureCredential
async def table_operations():
async with DefaultAzureCredential() as credential:
async with TableClient(
endpoint="https://<account>.table.core.windows.net",
table_name="mytable",
credential=credential
) as client:
await client.create_entity(entity={
"PartitionKey": "async",
"RowKey": "1",
"data": "test"
})
async for entity in client.query_entities("PartitionKey eq 'async'"):
print(entity)
import asyncio
asyncio.run(table_operations())
Data Types
| Python Type | Table Storage Type |
|---|
str | String |
int | Int64 |
float | Double |
bool | Boolean |
datetime | DateTime |
bytes | Binary |
UUID | Guid |
Best Practices
- Pick sync OR async and stay consistent. Do not mix
azure.data.tables sync clients with azure.data.tables.aio async clients in the same call path. Choose one mode per module.
- Always use context managers for clients and async credentials. Wrap every client in
with TableClient(...) as client: (sync) or async with TableClient(...) as client: (async). For async DefaultAzureCredential from azure.identity.aio, also use async with credential: so tokens and transports are cleaned up.
- Use
DefaultAzureCredential for portable auth across local dev and Azure (avoid connection strings / API keys when possible).
- Design partition keys for query patterns and even distribution
- Query within partitions whenever possible (cross-partition is expensive)
- Use batch operations for multiple entities in same partition
- Use
upsert_entity for idempotent writes
- Use parameterized queries to prevent injection
- Keep entities small — max 1MB per entity
- Use async client for high-throughput scenarios