All Products
Search
Document Center

Alibaba Cloud SDK:Integrate with Alibaba Cloud SDK V1.0 for Python

Last Updated:May 20, 2025

To facilitate API calls, we recommend that you integrate with Alibaba Cloud SDK in your project. SDKs simplify the development process, quickly integrate features, and greatly reduce the O&M cost. This topic describes how to integrate Alibaba Cloud SDK V1.0 for Python in your project and use the SDK for development.

Prerequisites

Python 3.7 or later is installed.

Install the SDK

When you install Alibaba Cloud SDK V1.0 for Python, the core library is automatically installed. Therefore, you only need to install the SDK.

Cloud service SDK

The SDK of a cloud service includes the request object that is required for calling API operations and the response object. The following example shows how t install the SDK of Elastic Compute Service (ECS). Run the following installation command on your terminal or command-line interface (CLI):

pip install aliyun-python-sdk-ecs

The SDK V1.0 for Python of an Alibaba Cloud service is named in the aliyun-python-sdk-${Service code} format. You can also view the information about the SDK V1.0 for Python of a specific Alibaba Cloud Service in the SDK Center.

Core library of an SDK

The core library of an SDK includes the client object, the signature logic, and the error handling logic, which are required information for calling API operations. To make generic calls or install the core library of a specific version, run the following command:

pip install aliyun-python-sdk-core

For more information about core library versions, see aliyun-python-sdk-core · PyPI.

Use the SDK

The following example shows how to use Alibaba Cloud SDK V1.0 to call the DescribeInstances API operation of ECS.

1. Initialize a request client

All API operations are called by using the Client object provided by the core library. Before you call an API operation, you must initialize a request client. In this example, the request client is initialized by using an AccessKey pair. For more information, see Manage access credentials.

Note

In this example, the AccessKey pair is obtained from the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, which must be configured before you run the code. For more information, see Configure environment variables in Linux, macOS, and Windows.

import os

from aliyunsdkcore.client import AcsClient

# Initialize an SDK client.
client = AcsClient(
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),  # Obtain the AccessKey ID of the Resource Access Management (RAM) user from environment variables.
    os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),  # Obtain the AccessKey secret of the RAM user from environment variables.
    '<region_id>' # The region ID.
)

2. Create a request object

You can use the request object provided by the SDK to encapsulate the request parameters.

Note

Name the request object of the API operation in the following format: <API operation name>Request.

from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest

# Create a DescribeInstancesRequest object.
request = DescribeInstancesRequest()
request.set_InstanceIds("[\"i-bp1dXXXXXXXXXXXX\"]")
request.set_PageSize(100)
request.set_PageNumber(1)

3. Initiate an API request

Use the request client built in Step 1 to call the do_action_with_exceptio operation. The request parameters of the operation are encapsulated in the request object created in Step 2. If the call is successful, a response object is returned.

response = client.do_action_with_exception(request)
print(response)

4. Handle errors

If an error is triggered when you can an API operation, you can capture ServerException and ClientException to obtain the error message. In most cases, ClientException errors are caused by invalid parameters during client initialization, such as invalid AccessKey parameters. ServerException errors may be caused by several factors. You can request customer service for troubleshooting and provide the request ID.

Complete sample code

import os

from aliyunsdkcore.acs_exception.exceptions import ClientException, ServerException
from aliyunsdkcore.client import AcsClient
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest

try:
    # Initialize an SDK client.
    client = AcsClient(
        os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),  # Obtain the AccessKey ID of the Resource Access Management (RAM) user from environment variables.
        os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),  # Obtain the AccessKey secret of the RAM user from environment variables.
        'cn-hangzhou' # The ID of the region. In this example, cn-hangzhou is used.
    )

    # Create the DescribeInstancesRequest object.
    request = DescribeInstancesRequest()
    request.set_InstanceIds("[\"i-bp1dXXXXXXXXXXXX\"]")
    request.set_PageSize(100)
    request.set_PageNumber(1)

    # Send the request.
    response = client.do_action_with_exception(request)
    print(response)
except ClientException as e:
    # Handle errors with caution in actual business scenarios and do not ignore errors in your project. In this example, error messages are only displayed. 
    print(e)
except ServerException as e:
    # Handle errors with caution in actual business scenarios and do not ignore errors in your project. In this example, error messages are only displayed. 
    print(e)

References

  • For more information about advanced SDK settings, such as proxy settings and timeout settings, see Advanced settings.

  • For more information about how to create an AccessKey pair, see Create an AccessKey pair.