Integrate an SDK
Integrate the V1.0 Python SDK into your project to simplify OpenAPI calls, speed up feature integration, and reduce maintenance costs.
Environment requirements
Python >= 3.7
Install the SDK
Installing a product SDK automatically installs the core SDK. You only need to install the product SDK.
Product SDK
A product SDK provides the Request and Response objects for OpenAPI calls. For example, install the ECS SDK:
pip install aliyun-python-sdk-ecs
The naming convention for a V1.0 SDK is aliyun-python-sdk-${product_code}. You can also find V1.0 SDKs for specific products in the SDK Center.
Core SDK
The core SDK provides the Client object, request signing, and exception handling. To make generalized calls or install a specific core SDK version, run:
pip install aliyun-python-sdk-core
View available versions on aliyun-python-sdk-core · PyPI.
Use the SDK
The following example calls the ECS DescribeInstances API with the V1.0 SDK.
1. Initialize the request client
All OpenAPI calls go through the Client object in the core SDK. Initialize the client before calling any API. This example uses an AccessKey pair. For other credential types, see Manage access credentials.
Before running the code, set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. Configure environment variables on Linux, macOS, and Windows.
import os
from aliyunsdkcore.client import AcsClient
# Initialize the client
client = AcsClient(
os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'), # Get the AccessKey ID of the RAM user from an environment variable
os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'), # Get the AccessKey secret of the RAM user from an environment variable
'<REGION_ID>' # Region ID
)
2. Construct an OpenAPI request parameter object
Use the Request object from the product SDK to set request parameters.
The naming convention for an OpenAPI request parameter object is <OpenAPIName>Request.
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
# Construct a DescribeInstancesRequest object
request = DescribeInstancesRequest()
request.set_InstanceIds("[\"i-bp1dXXXXXXXXXXXX\"]")
request.set_PageSize(100)
request.set_PageNumber(1)
3. Send the request
Call do_action_with_exception on the client with the Request object from Step 2. A successful call returns a Response object.
response = client.do_action_with_exception(request)
print(response)
4. Handle exceptions
Catch ClientException and ServerException to handle errors. A ClientException typically indicates incorrect initialization parameters such as an invalid AccessKey. A ServerException has various causes — provide the RequestId from the exception when contacting Alibaba Cloud support.
Configure a pip mirror source in the Chinese mainland (Optional)
Use an Alibaba Cloud mirror to speed up pip downloads in the Chinese mainland.
Temporary Use
Append -i with the mirror URL to a pip command for one-time use. Example with aliyun-python-sdk-core:
pip install aliyun-python-sdk-core -i https://mirrors.aliyun.com/pypi/simple/
Permanent Configuration
To set a permanent mirror, edit the pip configuration file: ~/.pip/pip.conf on Linux/macOS or %USERPROFILE%\pip\pip.ini on Windows. Create the file if it does not exist.
Linux/macOS
-
Open or create the configuration file:
mkdir -p ~/.pip && vim ~/.pip/pip.conf -
Press
ito enter edit mode, then add the following content:[global] index-url = http://mirrors.cloud.aliyuncs.com/pypi/simple/ -
Press
ESC, type:wq, and press Enter to save. Verify the configuration:pip config list # If the following information is displayed, the configuration is complete. global.index-url='http://mirrors.cloud.aliyuncs.com/pypi/simple/'
Windows
Create a pip.ini file in the %USERPROFILE%\pip folder with the following content:
[global]
index-url = http://mirrors.cloud.aliyuncs.com/pypi/simple/
References
-
Configure proxies, timeouts, and other options in Advanced configurations.