This topic describes how to install SDK for Python and provides examples of how to use SDK for Python.

Background information

  • To use SDK for Python, you must install the core library of Alibaba Cloud SDK for Python (aliyun-python-sdk-core) and Tag SDK for Java (aliyun-python-sdk-tag).
  • Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to debug API operations and dynamically generate SDK sample code.
  • For information about Tag API operations, see Tag management.

Install SDK for Python

Use the pip command. Alternatively, download SDK packages and add them to your project.

  • (Recommended) Use pip
    1. Run the following command to install the core library of Alibaba Cloud SDK for Python:
      pip install aliyun-python-sdk-core
    2. Run the following command to install Tag SDK for Python:
      pip install aliyun-python-sdk-tag
  • Download SDK packages and add them to your project

    You can download the SDK packages from the following links:

Use SDK for Python

The following examples provide the sample code of only some API operations. For information about the sample code of other API operations, visit OpenAPI Explorer. Then, find the target API operation, debug the operation, and obtain sample code.

  • Create and bind a tag
    #! /usr/bin/env python
    #coding=utf-8
    
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.acs_exception.exceptions import ClientException
    from aliyunsdkcore.acs_exception.exceptions import ServerException
    from aliyunsdktag.request.v20180828.TagResourcesRequest import TagResourcesRequest
    # Construct an Alibaba Cloud client to send requests.
    # When you construct the client, specify the region ID, AccessKey ID, and AccessKey secret.
    client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-qingdao')
    
    # Construct the request object.
    request = TagResourcesRequest()
    
    # Specify the required parameters.
    request.set_accept_format('json')
    request.set_Tags("{<tagKey>:<tagValue>}")
    request.set_ResourceARNs(["<resourceARN>"])
    
    # Issue the request and obtain a response.
    response = client.do_action_with_exception(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))                    
  • Query tags
    #! /usr/bin/env python
    #coding=utf-8
    
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.acs_exception.exceptions import ClientException
    from aliyunsdkcore.acs_exception.exceptions import ServerException
    from aliyunsdktag.request.v20180828.ListTagResourcesRequest import ListTagResourcesRequest
    # Construct an Alibaba Cloud client to send requests.
    # When you construct the client, specify the region ID, AccessKey ID, and AccessKey secret.
    client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-qingdao')
    
    # Construct the request object.
    request = ListTagResourcesRequest()
    
    # Specify the required parameters. You can specify the Alibaba Cloud Resource Names (ARNs) of different types of resources.  
    request.set_accept_format('json')
    request.set_ResourceARNs(["arn:acs:ecs:cn-qingdao:<AccountId>:instance/<ResourceId>","arn:acs:vpc:cn-qingdao:<AccountId>:vpc/<ResourceId>"])
    
    # Issue the request and obtain a response.
    response = client.do_action_with_exception(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))