# Azure SDK for Python

Important

The most part of this documentation has moved to https://docs.microsoft.com/python/azure

The Azure SDK for Python is a set of libraries which allow you to work on Azure for your management, runtime or data needs.

For a more general view of Azure and Python, you can go on the Python Developer Center for Azure

## Example Usage

This example shows:

- Authentication on Azure using an AD in your subscription,
- Creation of a Resource Group and a Storage account,
- Upload a simple “Hello world” HTML page and gives you the URL to get it.

    from azure.common.credentials import UserPassCredentials
    from azure.mgmt.resource import ResourceManagementClient
    from azure.mgmt.storage import StorageManagementClient
    from azure.storage import CloudStorageAccount
    from azure.storage.blob.models import ContentSettings, PublicAccess

    credentials = UserPassCredentials('user@domain.com', 'my_smart_password')
    subscription_id = '33333333-3333-3333-3333-333333333333'

    resource_client = ResourceManagementClient(credentials, subscription_id)
    storage_client = StorageManagementClient(credentials, subscription_id)

    resource_group_name = 'my_resource_group'
    storage_account_name = 'myuniquestorageaccount'

    resource_client.resource_groups.create_or_update(
        resource_group_name,
        {
            'location':'westus'
        }
    )

    async_create = storage_client.storage_accounts.create(
        resource_group_name,
        storage_account_name,
        {
            'location':'westus',
            'kind':'storage',
            'sku': {
                'name':'standard_ragrs'
            }
        }
    )
    async_create.wait()

    storage_keys = storage_client.storage_accounts.list_keys(resource_group_name, storage_account_name)
    storage_keys = {v.key_name: v.value for v in storage_keys.keys}

    storage_client = CloudStorageAccount(storage_account_name, storage_keys['key1'])
    blob_service = storage_client.create_block_blob_service()

    blob_service.create_container(
        'mycontainername',
        public_access=PublicAccess.Blob
    )

    blob_service.create_blob_from_bytes(
        'mycontainername',
        'myblobname',
        b'<center><h1>Hello World!</h1></center>',
        content_settings=ContentSettings('text/html')
    )

    print(blob_service.make_blob_url('mycontainername', 'myblobname'))

## Installation

### Overview

You can install individually each library for each Azure service:

    $ pip install azure-batch          # Install the latest Batch runtime library
    $ pip install azure-mgmt-scheduler # Install the latest Storage management library

Preview packages can be installed using the `--pre` flag:

    $ pip install --pre azure-mgmt-compute # will install only the latest Compute Management library

More details and information about the available libraries and their status can be found in the Installation Page

### The `azure` meta-package

You can also install a set of Azure libraries in a single line using the `azure` meta-package. Since not all packages in this meta-package are published as stable yet, the `azure` meta-package is still in preview. However, the core packages, from code quality/completeness perspectives can at this time be considered “stable” - it will be officially labeled as such in sync with other languages as soon as possible. We are not planning on any further major changes until then.

Since it’s a preview release, you need to use the `--pre` flag:

    $ pip install --pre azure

or directly

    $ pip install azure==2.0.0rc6

Important

The azure meta-package 1.0.3 is deprecated and is not working anymore.

## Features

### Azure Resource Management

All documentation of management libraries for Azure are on this website. This includes:

- Authorization : Permissions, roles and more
- Batch : Manage Batch accounts and applications
- Content Delivery Network : Profiles, endpoints creation and more
- Cognitive Services : Create CS accounts and more
- Commerce - Billing API : RateCard and Usage Billing API
- Compute : Create virtual machines and more
- Data Lake Analytics : Manage account, job, catalog and more
- Data Lake Store : Manage account and more
- DevTestLabs : Create labs and more
- DNS : Create DNS zone, record set and more
- IoTHub : Create IoTHub account and more
- KeyVault : Create vaults and more
- App Service : Create App plan, Web Apps, Logic Apps and more
- Media Services : Create account and more
- Network : Create virtual networks, network interfaces, public IPs and more
- Notification Hubs : Namespaces, hub creation/deletion and more
- PowerBI Embedded : Create account and more
- Redis Cache : Create cache and more
- Resource Management:
  - resources : create resource groups, register providers and more
  - features : manage features of provider and more
  - locks : manage resource group lock and more
  - subscriptions : manage subscriptions and more
- Scheduler : Create job collections, create job and more
- Server Manager : Create gateways, nodes and more
- SQL Database : Create servers, databases nodes and more
- Storage : Create storage accounts, list keys, and more
- Traffic Manager : Create endpoints, profiles and more

### Azure Runtime

Some documentation of data libraries are on this website. This includes:

- Batch

- Key Vault

- Azure Monitor

- Azure Active Directory Graph RBAC

- Service Bus using HTTP.

  Note

  For critical performance issue, the Service Bus team currently recommends AMQP.

These Azure services have Python data libraries which are directly hosted by the service team or are extensively documented on the Azure documentation website:

- Storage
- Azure Data Lake Store Filesystem
- Azure IoT Hub service and device SDKs for Python
- SQL Azure
- DocumentDB
- Application Insight
- Redis Cache
- Write an Azure WebApp in Python

### Azure Service Management

Note

The Service Management SDK is deprecated and no more features will be added.

This page describes the usage and detailed features of Azure Service Management SDK. At a glance:

> - storage accounts: create, update, delete, list, regenerate keys
> - affinity groups: create, update, delete, list, get properties
> - locations: list
> - hosted services: create, update, delete, list, get properties
> - deployment: create, get, delete, swap, change configuration, update status, upgrade, rollback
> - role instance: reboot, reimage
> - discover addresses and ports for the endpoints of other role instances in your service
> - get configuration settings and access local resources
> - get role instance information for current role and other role instances
> - query and set the status of the current role

## System Requirements:

The supported Python versions are 2.7.x, 3.3.x, 3.4.x, 3.5.x and 3.6.x To download Python, please visit https://www.python.org/download/

We recommend Python Tools for Visual Studio as a development environment for developing your applications. Please visit http://aka.ms/python for more information.

## Need Help?:

Be sure to check out the Microsoft Azure Developer Forums on Stack Overflow if you have trouble with the provided code.

## Contribute Code or Provide Feedback:

If you would like to become an active contributor to this project please follow the instructions provided in Microsoft Azure Projects Contribution Guidelines.

If you encounter any bugs with the library please file an issue in the Issues section of the project.

## Indices and tables

- Index
- Module Index
- Search Page

User Documentation

- Installation
- Resource Management Authentication
- Multi-cloud - use Azure on all regions
  - Using predeclared cloud definition
  - Using your own cloud definition (e.g. Azure Stack)
  - Using ADAL
    - Azure Government
    - Azure Germany
    - Azure China
- Exception handling
  - Asynchronous operation
  - Raw operation

Management Documentation

- Resource Management
- Apps Management
- Batch Management
  - Create the Batch Management client
  - Create a Batch Account
  - Application Packages
- Content Delivery Network Management
  - Create the management client
  - Check name availability
- Compute and Network Management
- Redis Cache Management
  - Create the management client
  - Create Redis Cache
- Scheduler Management
  - Create the management client
  - Create a job collection
- Storage Management
- Authorization Management
  - Create the management client
  - Check permissions for a resource group
- Cognitive Services Management
  - Create the management client
- Commerce - Billing API
  - Create the commerce client
  - Get rate card
  - Get Usage
- Azure Data Lake Management Analytics
  - Create the management client
  - Samples
- Azure Data Lake Management Store
  - Create the management client
  - Samples
- DevTestLabs Management
  - Create the management client
  - Create lab
- DNS Management
  - Create the management client
  - Create DNS zone
  - Create a Record Set
- IoTHub Management
  - Create the management client
  - Create an IoTHub
- KeyVault Management
  - Create the management client
  - Samples usage
- Managed Disks
- Notification Hubs Management
  - Create the management client
  - Check namespace availability
- PowerBI Embedded Management
  - Create the management client
- ServerManager Management
  - Create the management client
  - Create gateway
- SQL Database Management
  - Create the management client
  - Servers/Databases sample
- Traffic Manager Management
  - Create the management client
- Service Management (Legacy)
  - Usage
    - Set-up certificates
    - Using the Azure .PublishSettings certificate
    - Creating and uploading new certificate with OpenSSL
    - Initialization
    - List Available Locations
    - Create a Storage Service
    - Create a Cloud Service
    - Create a Deployment

Runtime Documentation

- Batch
  - Create the Batch client
    - Shared Key Authentication
    - Azure Active Directory Authentication
  - Manage Pools and Nodes
  - Manage Jobs and Tasks
- Azure Monitor
  - Create the Monitor client
  - Registration
  - Get the Activity Log
  - Metrics
  - Alert rules
- KeyVault
  - Create the client
  - Access policies
  - Example
- Azure Active Directory Graph Rbac API
  - Create the client
  - Manage users
- Service Bus
  - ServiceBus Queues
    - Shared Access Signature Authentication
    - ACS Authentication
    - Sending and Receiving Messages
  - ServiceBus Topics
  - Event Hub
  - Advanced features
    - Broker Properties and User Properties

Developer Documentation

- azure.common package
  - Submodules
  - Module contents
- azure.batch package
  - Submodules
    - azure.batch.models module
    - azure.batch.operations module
  - Module contents
- azure.graphrbac package
  - Submodules
    - azure.graphrbac.models module
    - azure.graphrbac.operations module
  - Module contents
- azure.keyvault package
  - Submodules
    - azure.keyvault.models module
    - azure.keyvault.operations module
  - Module contents
- azure.mgmt.authorization package
  - Submodules
    - azure.mgmt.authorization.models module
    - azure.mgmt.authorization.operations module
  - Module contents
- azure.mgmt.batch package
  - Submodules
    - azure.mgmt.batch.models module
    - azure.mgmt.batch.operations module
  - Module contents
- azure.mgmt.billing package
  - Submodules
    - azure.mgmt.billing.models module
    - azure.mgmt.billing.operations module
  - Module contents
- azure.mgmt.cdn package
  - Submodules
    - azure.mgmt.cdn.models module
    - azure.mgmt.cdn.operations module
  - Module contents
- azure.mgmt.cognitiveservices package
  - Submodules
    - azure.mgmt.cognitiveservices.models module
    - azure.mgmt.cognitiveservices.operations module
  - Module contents
- azure.mgmt.commerce package
  - Submodules
    - azure.mgmt.commerce.models module
    - azure.mgmt.commerce.operations module
  - Module contents
- azure.mgmt.compute.compute package
  - Module contents
  - Submodules
    - azure.mgmt.compute.compute.v2016_04_30_preview package
      - Submodules
        - azure.mgmt.compute.compute.v2016_04_30_preview.models module
        - azure.mgmt.compute.compute.v2016_04_30_preview.operations module
      - Module contents
    - azure.mgmt.compute.compute.v2016_03_30 package
      - Submodules
        - azure.mgmt.compute.compute.v2016_03_30.models module
        - azure.mgmt.compute.compute.v2016_03_30.operations module
      - Module contents
    - azure.mgmt.compute.compute.v2015_06_15 package
      - Submodules
        - azure.mgmt.compute.compute.v2015_06_15.models module
        - azure.mgmt.compute.compute.v2015_06_15.operations module
      - Module contents
- azure.mgmt.compute.containerservice package
  - Module contents
  - Submodules
    - azure.mgmt.compute.containerservice.v2017_01_31 package
      - Submodules
        - azure.mgmt.compute.containerservice.v2017_01_31.models module
        - azure.mgmt.compute.containerservice.v2017_01_31.operations module
      - Module contents
- azure.mgmt.consumption package
  - Submodules
    - azure.mgmt.consumption.models module
    - azure.mgmt.consumption.operations module
  - Module contents
- azure.mgmt.containerregistry.v2017_03_01 package
  - Submodules
    - azure.mgmt.containerregistry.v2017_03_01.models module
    - azure.mgmt.containerregistry.v2017_03_01.operations module
  - Module contents
- azure.mgmt.containerregistry.v2017_06_01_preview package
  - Submodules
    - azure.mgmt.containerregistry.v2017_06_01_preview.models module
    - azure.mgmt.containerregistry.v2017_06_01_preview.operations module
  - Module contents
- azure.mgmt.datalake.analytics.account package
  - Submodules
    - azure.mgmt.datalake.analytics.account.models module
    - azure.mgmt.datalake.analytics.account.operations module
  - Module contents
- azure.mgmt.datalake.analytics.catalog package
  - Submodules
    - azure.mgmt.datalake.analytics.catalog.models module
    - azure.mgmt.datalake.analytics.catalog.operations module
  - Module contents
- azure.mgmt.datalake.analytics.job package
  - Submodules
    - azure.mgmt.datalake.analytics.job.models module
    - azure.mgmt.datalake.analytics.job.operations module
  - Module contents
- azure.mgmt.datalake.store package
  - Submodules
    - azure.mgmt.datalake.store.models module
    - azure.mgmt.datalake.store.operations module
  - Module contents
- azure.mgmt.devtestlabs package
  - Submodules
    - azure.mgmt.devtestlabs.models module
    - azure.mgmt.devtestlabs.operations module
  - Module contents
- azure.mgmt.dns package
  - Submodules
    - azure.mgmt.dns.models module
    - azure.mgmt.dns.operations module
  - Module contents
- azure.mgmt.documentdb package
  - Submodules
    - azure.mgmt.documentdb.models module
    - azure.mgmt.documentdb.operations module
  - Module contents
- azure.mgmt.eventhub package
  - Submodules
    - azure.mgmt.eventhub.models module
    - azure.mgmt.eventhub.operations module
  - Module contents
- azure.mgmt.iothub package
  - Submodules
    - azure.mgmt.iothub.models module
    - azure.mgmt.iothub.operations module
  - Module contents
- azure.mgmt.keyvault package
  - Submodules
    - azure.mgmt.keyvault.models module
    - azure.mgmt.keyvault.operations module
  - Module contents
- azure.mgmt.logic package
  - Submodules
    - azure.mgmt.logic.models module
    - azure.mgmt.logic.operations module
  - Module contents
- azure.mgmt.media package
  - Submodules
    - azure.mgmt.media.models module
    - azure.mgmt.media.operations module
  - Module contents
- azure.mgmt.monitor package
  - Submodules
    - azure.mgmt.monitor.models module
    - azure.mgmt.monitor.operations module
  - Module contents
- azure.mgmt.network package
  - Module contents
  - Submodules
    - azure.mgmt.network.v2017_03_01 package
      - Submodules
        - azure.mgmt.network.v2017_03_01.models module
        - azure.mgmt.network.v2017_03_01.operations module
      - Module contents
    - azure.mgmt.network.v2016_12_01 package
      - Submodules
        - azure.mgmt.network.v2016_12_01.models module
        - azure.mgmt.network.v2016_12_01.operations module
      - Module contents
    - azure.mgmt.network.v2016_09_01 package
      - Submodules
        - azure.mgmt.network.v2016_09_01.models module
        - azure.mgmt.network.v2016_09_01.operations module
      - Module contents
    - azure.mgmt.network.v2015_06_15 package
      - Submodules
        - azure.mgmt.network.v2015_06_15.models module
        - azure.mgmt.network.v2015_06_15.operations module
      - Module contents
- azure.mgmt.notificationhubs package
  - Submodules
    - azure.mgmt.notificationhubs.models module
    - azure.mgmt.notificationhubs.operations module
  - Module contents
- azure.mgmt.powerbiembedded package
  - Submodules
    - azure.mgmt.powerbiembedded.models module
    - azure.mgmt.powerbiembedded.operations module
  - Module contents
- azure.mgmt.rdbms.mysql package
  - Submodules
    - azure.mgmt.rdbms.mysql.models module
    - azure.mgmt.rdbms.mysql.operations module
  - Module contents
- azure.mgmt.rdbms.postgresql package
  - Submodules
    - azure.mgmt.rdbms.postgresql.models module
    - azure.mgmt.rdbms.postgresql.operations module
  - Module contents
- azure.mgmt.recoveryservicesbackup package
  - Submodules
    - azure.mgmt.recoveryservicesbackup.models module
    - azure.mgmt.recoveryservicesbackup.operations module
  - Module contents
- azure.mgmt.redis package
  - Submodules
    - azure.mgmt.redis.models module
    - azure.mgmt.redis.operations module
  - Module contents
- azure.mgmt.resource.features package
  - Module contents
  - Submodules
    - azure.mgmt.resource.features.v2015_12_01 package
      - Submodules
        - azure.mgmt.resource.features.v2015_12_01.models module
        - azure.mgmt.resource.features.v2015_12_01.operations module
      - Module contents
- azure.mgmt.resource.links package
  - Module contents
  - Submodules
    - azure.mgmt.resource.links.v2016_09_01 package
      - Submodules
        - azure.mgmt.resource.links.v2016_09_01.models module
        - azure.mgmt.resource.links.v2016_09_01.operations module
      - Module contents
- azure.mgmt.resource.locks package
  - Module contents
  - Submodules
    - azure.mgmt.resource.locks.v2016_09_01 package
      - Submodules
        - azure.mgmt.resource.locks.v2016_09_01.models module
        - azure.mgmt.resource.locks.v2016_09_01.operations module
      - Module contents
    - azure.mgmt.resource.locks.v2015_01_01 package
      - Submodules
        - azure.mgmt.resource.locks.v2015_01_01.models module
        - azure.mgmt.resource.locks.v2015_01_01.operations module
      - Module contents
- azure.mgmt.resource.managedapplications package
  - Submodules
    - azure.mgmt.resource.managedapplications.models module
    - azure.mgmt.resource.managedapplications.operations module
  - Module contents
- azure.mgmt.resource.policy package
  - Module contents
  - Submodules
    - azure.mgmt.resource.policy.v2016_12_01 package
      - Submodules
        - azure.mgmt.resource.policy.v2016_12_01.models module
        - azure.mgmt.resource.policy.v2016_12_01.operations module
      - Module contents
    - azure.mgmt.resource.policy.v2016_04_01 package
      - Submodules
        - azure.mgmt.resource.policy.v2016_04_01.models module
        - azure.mgmt.resource.policy.v2016_04_01.operations module
      - Module contents
    - azure.mgmt.resource.policy.v2015_10_01_preview package
      - Submodules
        - azure.mgmt.resource.policy.v2015_10_01_preview.models module
        - azure.mgmt.resource.policy.v2015_10_01_preview.operations module
      - Module contents
- azure.mgmt.resource.resources package
  - Module contents
  - Submodules
    - azure.mgmt.resource.resources.v2017_05_10 package
      - Submodules
        - azure.mgmt.resource.resources.v2017_05_10.models module
        - azure.mgmt.resource.resources.v2017_05_10.operations module
      - Module contents
    - azure.mgmt.resource.resources.v2016_09_01 package
      - Submodules
        - azure.mgmt.resource.resources.v2016_09_01.models module
        - azure.mgmt.resource.resources.v2016_09_01.operations module
      - Module contents
    - azure.mgmt.resource.resources.v2016_02_01 package
      - Submodules
        - azure.mgmt.resource.resources.v2016_02_01.models module
        - azure.mgmt.resource.resources.v2016_02_01.operations module
      - Module contents
- azure.mgmt.resource.subscriptions package
  - Module contents
  - Submodules
    - azure.mgmt.resource.subscriptions.v2016_06_01 package
      - Submodules
        - azure.mgmt.resource.subscriptions.v2016_06_01.models module
        - azure.mgmt.resource.subscriptions.v2016_06_01.operations module
      - Module contents
- azure.mgmt.scheduler package
  - Submodules
    - azure.mgmt.scheduler.models module
    - azure.mgmt.scheduler.operations module
  - Module contents
- azure.mgmt.search package
  - Submodules
    - azure.mgmt.search.models module
    - azure.mgmt.search.operations module
  - Module contents
- azure.mgmt.servermanager package
  - Submodules
    - azure.mgmt.servermanager.models module
    - azure.mgmt.servermanager.operations module
  - Module contents
- azure.mgmt.servicebus package
  - Submodules
    - azure.mgmt.servicebus.models module
    - azure.mgmt.servicebus.operations module
  - Module contents
- azure.mgmt.sql package
  - Submodules
    - azure.mgmt.sql.models module
    - azure.mgmt.sql.operations module
  - Module contents
- azure.mgmt.storage package
  - Module contents
  - Submodules
    - azure.mgmt.storage.v2016_12_01 package
      - Submodules
        - azure.mgmt.storage.v2016_12_01.models module
        - azure.mgmt.storage.v2016_12_01.operations module
      - Module contents
    - azure.mgmt.storage.v2015_06_15 package
      - Submodules
        - azure.mgmt.storage.v2015_06_15.models module
        - azure.mgmt.storage.v2015_06_15.operations module
      - Module contents
- azure.mgmt.trafficmanager package
  - Submodules
    - azure.mgmt.trafficmanager.models module
    - azure.mgmt.trafficmanager.operations module
  - Module contents
- azure.mgmt.web package
  - Submodules
    - azure.mgmt.web.models module
    - azure.mgmt.web.operations module
  - Module contents
- azure.monitor package
  - Submodules
    - azure.monitor.models module
    - azure.monitor.operations module
  - Module contents
- azure.servicefabric package
  - Submodules
    - azure.servicefabric.models module
    - azure.servicefabric.operations module
  - Module contents
- azure.servicebus package
  - Submodules
    - azure.servicebus.constants module
    - azure.servicebus.models module
    - azure.servicebus.servicebusservice module
  - Module contents
- azure.servicemanagement package
  - Submodules
    - azure.servicemanagement.constants module
    - azure.servicemanagement.models module
    - azure.servicemanagement.publishsettings module
    - azure.servicemanagement.schedulermanagementservice module
    - azure.servicemanagement.servicebusmanagementservice module
    - azure.servicemanagement.servicemanagementclient module
    - azure.servicemanagement.servicemanagementservice module
    - azure.servicemanagement.sqldatabasemanagementservice module
    - azure.servicemanagement.websitemanagementservice module
  - Module contents