This topic describes how to install SDKs for Python and provides sample code.

Background information

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

Installation

To install the SDKs for Python, you can run the pip command, or download the installation packages of the SDKs and add the packages to your project.

  • Use the pip command (recommended)
    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 the installation packages of the SDKs and add the packages to your project

    You can download the installation packages of the SDKs from the following links:

Example

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

  • Add 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 that is used to initiate requests.
    # When you construct the client, specify a region ID and your AccessKey ID and AccessKey secret.
    client = AcsClient('<accessKeyId>', '<accessKeySecret>', 'cn-qingdao')
    
    # Construct the request object.
    request = TagResourcesRequest()
    
    # Specify request parameters. For more information about the parameters, see API Reference.
    request.set_accept_format('json')
    request.set_Tags("{<tagKey>:<tagValue>}")
    request.set_ResourceARNs(["<resourceARN>"])
    
    # Initiate 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 that is used to initiate requests.
    # When you construct the client, specify a region ID and your AccessKey ID and AccessKey secret.
    client = AcsClient('<accessKeyId>', '<accessKeySecret>', 'cn-qingdao')
    
    # Construct the request object.
    request = ListTagResourcesRequest()
    
    # Specify request parameters. You can specify the Alibaba Cloud Resource Names (ARNs) of different types of resources. For more information about the parameters, see API Reference.  
    request.set_accept_format('json')
    request.set_ResourceARNs(["arn:acs:ecs:cn-qingdao:<AccountId>:instance/<ResourceId>","arn:acs:vpc:cn-qingdao:<AccountId>:vpc/<ResourceId>"])
    
    # Initiate the request and obtain a response.
    response = client.do_action_with_exception(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))