Getting started

Last Updated: Nov 17, 2017

Welcome to the Alibaba Cloud Python Software Development Kit (SDK). The Alibaba Cloud Python SDK helps you build Python applications using Alibaba Cloud services such as ECS, OSS, RDS, and more, without complex coding required.

Step 1 Install Python SDK

Alibaba Cloud Python SDK supports Python 2.6.x and 2.7.x and Python 3.x. Run python --version to check your version of Python.

You can install the Alibaba Cloud Python SDK using the following two methods. Regardless of which method and cloud service are used, the core library aliyun-python-sdk-core must be installed. You can download the product SDKs from Python SDK resource list.

  • Install with pip

    pip is a commonly used package management tool for Python. If pip is not installed, see the pip user guide to install pip.

    Run the following command to install the individual Alibaba Cloud service's libraries:

    1. # Install the core library
    2. pip install aliyun-python-sdk-core
    3. # Install the ECS management library
    4. pip install aliyun-python-sdk-ecs
    5. # Install the RDS management library
    6. pip install aliyun-python-sdk-rds

    Note: If you are using Python 3.x, run the following command to install the core library:

    pip install aliyun-python-sdk-core-v3

  • Install from GitHub

    You can clone the source code to your local folder and then run setup.py install to install the SDK:

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

Step 2 Create an AccessKey

Before you start using Alibaba Cloud Python SDK, you must get the AccessKey of your account. The AccessKey is required when initializing the AcsClient instance. You can create an AccessKey in the Alibaba Cloud console. For more information, see Create an AccessKey.

Note: To increase the security of your account, we recommend that you use the AccessKey of the RAM account to access Alibaba Cloud services.

Step 3 Use Python SDK

  1. Import the required modules as follows.

    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. Initialize the AcsClient instance.

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

    where:

    • access-key-id is the Accesskey for your account.

    • access-key-secret is the access secret for your account.

    • region-id is the ID of the region where the service is called. For a list of region IDs, see Regions and zones.

      Note: The sequence of these parameters cannot be changed.

  3. Initialize a request and print response.

    1. # Initialize a request and set parameters
    2. request = DescribeInstancesRequest.DescribeInstancesRequest()
    3. request.set_PageSize(10)
    4. # Print response
    5. response = client.do_action_with_exception(request)
    6. print response

    Code example

    The following example shows how to query a list of ECS instances in a specified region using DescribeInstances. Substitute the values for your-access-key-id, your-access-key-secret, and your-region-id.

    1. # -*- coding: utf8 -*-
    2. from aliyunsdkcore.client import AcsClient
    3. from aliyunsdkcore.acs_exception.exceptions import ClientException
    4. from aliyunsdkcore.acs_exception.exceptions import ServerException
    5. from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest
    6. from aliyunsdkecs.request.v20140526 import StopInstanceRequest
    7. # Initialize AcsClient instance
    8. client = AcsClient(
    9. "<your-access-key-id>",
    10. "<your-access-key-secret>",
    11. "<your-region-id>"
    12. );
    13. # Initialize a request and set parameters
    14. request = DescribeInstancesRequest.DescribeInstancesRequest()
    15. request.set_PageSize(10)
    16. # Print response
    17. response = client.do_action_with_exception(request)
    18. print response
Thank you! We've received your feedback.