edit-icon download-icon

Use Python SDK

Last Updated: Mar 20, 2018

Install Python SDK

Alibaba Cloud Python SDK supports Python 2.6.x, 2.7.x, 3.x and later. You can install Python SDK using the following two methods:

  • Use pip (recommended)

    Run the following commands to install Python SDK using pip.

    1. pip install aliyun-python-sdk-core # Install Alibaba Cloud SDK core library
    2. pip install aliyun-python-sdk-ecs # Install ECS SDK
    3. pip install aliyun-python-sdk-rds # Install RDS SDK

    Note: If you use python3.x, modify pip install aliyun-python-sdk-core to pip install aliyun-python-sdk-core-v3.

  • Use GitHub

    Run the following commands to clone the Python SDK from GitHub and install the SDK:

    1. git clone https://github.com/aliyun/aliyun-openapi-python-sdk.git
    2. # Install Alibaba Cloud SDK core library
    3. cd aliyun-python-sdk-core
    4. python setup.py install
    5. # Install Alibaba Cloud ECS SDK
    6. cd aliyun-python-sdk-ecs
    7. python setup.py install

Set up credentials

When using the Alibaba Cloud Python SDK to access Alibaba Cloud services, you need an Alibaba Cloud account for authentication.

Python SDK supports the following authentication methods.

Authentications Descriptions
AccessKey Use the AccessKeyID/Secret to do the authentication
StsToken Use the STS Token to do the authentication
RamRoleArn Use the AssumeRole of the RAM account to do the authentication
EcsRamRole Use the RAM role of an ECS instance to do the authentication
RsaKeyPair Use the RSA key pair to do the authentication (supported only on Japan site)

This document uses AccessKey as an example to illustrate how to set up credentials.

Note: To ensure the security of your account, we recommend using your RAM account instead of the primary account. The primary account has full access to all of your cloud services, while the RAM account has limited access granted by the primary account to the cloud services.

Firstly, create an AccessKey as described in Create an AccessKey, and then set up your credentials when initializing AcsClient as follows:

Note: Do not disclose any code containing your AccessKey (do not commit the code to public GitHub projects). Otherwise, your Alibaba Cloud account may be compromised.

  1. client = AcsClient(
  2. "<access-key-id>",
  3. "<access-key-secret>",
  4. "<region-id>"
  5. );

Call a service

This document uses ECS as an example to show you how to use Alibaba Cloud Python SDK to send a request.

  1. Import the SDKs of the product.

    1. from aliyunsdkcore.client import AcsClient
    2. from aliyunsdkcore.acs_exception.exceptions import ClientException
    3. from aliyunsdkcore.acs_exception.exceptions import ServerException
    4. from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest
    5. from aliyunsdkecs.request.v20140526 import StopInstanceRequest
  2. Create an AcsClient.

    1. client = AcsClient(
    2. "<your-access-key-id>",
    3. "<your-access-key-secret>",
    4. "<your-region-id>"
    5. );
  3. Create a request and assign values to parameters in the request.

    1. request = DescribeInstancesRequest.DescribeInstancesRequest()
    2. request.set_PageSize(10)
  4. Initiate a call and print the response.

    1. response = client.do_action_with_exception(request)
    2. print response
Thank you! We've received your feedback.