This topic describes how to install and use the Alibaba Cloud SDK for Python.

Install Alibaba Cloud SDK for Python

The Alibaba Cloud SDK for Python supports Python 2.6.x, 2.7.x, 3.x, and later. You can install the Alibaba Cloud SDK for Python by using pip or GitHub.

  • Use pip (recommended)

    Run the following commands to install the Alibaba Cloud SDK for Python by using pip:
    pip install aliyun-python-sdk-core # Install the Alibaba Cloud SDK for Python core library.
    pip install aliyun-python-sdk-rds # Install the ECS management library.
    Note If you are using Python 3.x, use the pip install aliyun-python-sdk-core-v3 command instead of the pip install aliyun-python-sdk-core command.
  • Download GitHub source code

    Run the following commands to install the Alibaba Cloud SDK for Python from GitHub:
    git clone https://github.com/aliyun/aliyun-openapi-python-sdk.git
    # Install the Alibaba Cloud SDK for Python core library.
    cd aliyun-python-sdk-core
    python setup.py install
    # Install ECS SDK library.
    cd aliyun-python-sdk-ecs
    python setup.py install

Configure authentication credentials

When using Alibaba Cloud SDK for Python to access Alibaba Cloud services, you must provide an authorized Alibaba Cloud account for authentication.

The Alibaba Cloud SDK for Python supports the following authentication methods:

Authentication method Description
AccessKey Use the AccessKey ID and AccessKey Secret for authentication.
StsToken Use the STS Token for authentication.
RamRoleArn Use the AssumeRole of a RAM user for authentication.
EcsRamRole Use the EcsRamRole of an ECS instance for password-free authentication.
RsaKeyPair Use the RSA key pair for authentication (supported only on Japan site).

The following section uses an AccessKey pair as an example to describe how to configure an identity credential. To ensure the security of your account, we recommend that you use a RAM user to access Alibaba Cloud services. The AccessKey pair of an Alibaba Cloud account has full permissions on its resources. A RAM user is created under the authorization of an Alibaba Cloud account and has only limited operation permissions on specific resources. For more information, see Create an AccessKey pair for a RAM user.

If you want to use an AccessKey pair as the access credential, you must configure the AccessKey pair when you initialize the client.
Notice Do not disclose any code that may contain your AccessKey pair. For example, do not commit the code to public GitHub projects. Otherwise, your Alibaba Cloud account may be compromised.
client = AcsClient(
   "<access-key-id>", 
   "<access-key-secret>",
   "<region-id>")

Initiate a request

The following section uses ECS as an example to describe how to use the Alibaba Cloud SDK for Python to initiate a request.

  1. Import the SDK library of the service that you want to call.
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.acs_exception.exceptions import ClientException
    from aliyunsdkcore.acs_exception.exceptions import ServerException
    from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest
    from aliyunsdkecs.request.v20140526 import StopInstanceRequest
  2. Create an AcsClient object.
    client = AcsClient(
       "<your-access-key-id>", 
       "<your-access-key-secret>",
       "<your-region-id>"
    );
  3. Create a request object.
    request = DescribeInstancesRequest.DescribeInstancesRequest()
    request.set_PageSize(10)
  4. Initiate the request and handle the response.
    try:
       response = client.do_action_with_exception(request)
       print response
    except ServerException as e:
       print e
    except ClientException as e:
    print e