Learn how to use the Alibaba Cloud Python SDK with PyCharm on Windows.
Prerequisites
-
Python is installed. Install Python.
-
PyCharm is installed. Set up a Python development environment on Windows.
Use the SDK
Use the sample project from OpenAPI Portal
If you cannot download the sample project from OpenAPI Portal, follow Use the SDK in an existing project.
-
Go to the API Debugging page in the OpenAPI Portal. Select a product and an API. For example, to call the DescribeInstanceTypeFamilies API of Elastic Compute Service (ECS), enter `DescribeInstanceTypeFamilies` in the search bar and go to the API debugging page.

-
On the Parameter Settings tab, specify the required parameters. When you specify the parameters, refer to the Documentation tab on the right to understand the API description, notes, and the meaning and usage of each parameter. Pay special attention to information about fees. For example, the DescribeInstanceTypeFamilies API supports two parameters. `RegionId` is required. You can enter `cn-hangzhou` for the China (Hangzhou) region. `Generation` is optional. You can enter `ecs-5` for instance family V. For more information about parameter values, see the documentation on the right.

-
On the SDK Sample tab on the right, select a language. Click the Download Project button to download the complete SDK project to your local computer and decompress it.

-
Open PyCharm. Click File > Open, select the unzipped project folder, and click OK in the Creating Virtual Environment dialog. Wait for PyCharm to create the virtual environment and download dependencies.
NoteIf dependencies fail to download, run
python3 setup.py installin the terminal. -
Before you make a call, obtain an AccessKey. Use the AccessKey of a Resource Access Management (RAM) user. For more information, see Create an AccessKey for a RAM user.
ImportantAfter you obtain the AccessKey for the RAM user, you must set the AccessKey in the environment variables. For more information, see Configure environment variables on Linux, macOS, and Windows systems.
-
Run the sample code.
Click the Terminal tab at the bottom of PyCharm or press
Alt + F12. Run the following command:python ./alibabacloud_sample/sample.py
-
View the result. In the console at the bottom, press
Ctrl+Fand search forstatusCode. If"statusCode": 200is displayed, the call is successful.
Use the SDK in an existing project
-
Obtain the SDK.
Go to SDK Center and select a product, such as Elastic Compute Service. Set SDK Version to V2.0 and Language to Python.

-
Install the SDK.
In PyCharm, press
Alt+F12to open the terminal. Paste the installation command and press Enter.
-
Create a Python file.
Right-click the project name and select New > Python File. Enter `sdk_demo` as the file name, select Python File, and press Enter.
-
Initialize the client.
Initialize the ECS client before calling an API.
ImportantYou must use an AccessKey for identity verification when you initialize the client. For information about how to obtain an AccessKey, see Create an AccessKey.
After you obtain the AccessKey for the RAM user, you must set the AccessKey in the environment variables. For more information, see Configure environment variables on Linux, macOS, and Windows systems.
For information about how to set the endpoint, see Endpoints.
import os from alibabacloud_ecs20140526 import client as ecs_client from alibabacloud_tea_openapi import models as open_api_models def init_ecs_client(): """ Initializes the ECS client. This function takes no arguments. Returns: ecs_client.Client: An initialized ECS client object for further ECS operations. """ # Create an ECS configuration object and read the AccessKey pair from environment variables. ecs_config = open_api_models.Config() ecs_config.access_key_id = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'] ecs_config.access_key_secret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] # Set the endpoint. ecs_config.endpoint = 'ecs-cn-hangzhou.aliyuncs.com' # Initialize the ECS client with the configuration and return it. return ecs_client.Client(ecs_config) if __name__ == '__main__': client = init_ecs_client() -
Call the API. Review the API Documentation for the target API. The following example calls the `DescribeRegions` API of ECS.
NoteEach API has a separate request object that follows the `${APIName}${Request}` naming convention, such as `DescribeRegionsRequest`.
import os from alibabacloud_ecs20140526 import client as ecs_client from alibabacloud_tea_openapi import models as open_api_models from alibabacloud_ecs20140526 import models as ecs_20140526_models def init_ecs_client(): """ Initializes the ECS client. This function takes no arguments. Returns: ecs_client.Client: An initialized ECS client object for further ECS operations. """ # Create an ECS configuration object and read the AccessKey pair from environment variables. ecs_config = open_api_models.Config() ecs_config.access_key_id = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'] ecs_config.access_key_secret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] # Set the endpoint. ecs_config.endpoint = 'ecs-cn-hangzhou.aliyuncs.com' # Initialize the ECS client with the configuration and return it. return ecs_client.Client(ecs_config) if __name__ == '__main__': # Initialize the ECS client. client = init_ecs_client() # Create a DescribeRegionsRequest object. describeRegions_request = ecs_20140526_models.DescribeRegionsRequest() # Send a describeRegions request to get region information. response = client.describe_regions(describeRegions_request) print(response.body) -
Handle exceptions.
The Python V2.0 SDK handles exceptions through `Tea.exceptions` with two exception types:
-
`UnretryableException`: Thrown when the maximum number of retries is reached due to network issues.
-
`TeaException`: Indicates a service error during an SDK request.
import os from Tea.exceptions import UnretryableException, TeaException from alibabacloud_ecs20140526 import client as ecs_client from alibabacloud_tea_openapi import models as open_api_models from alibabacloud_ecs20140526 import models as ecs_20140526_models def init_ecs_client(): """ Initializes the ECS client. This function takes no arguments. Returns: ecs_client.Client: An initialized ECS client object for further ECS operations. """ # Create an ECS configuration object and read the AccessKey pair from environment variables. ecs_config = open_api_models.Config() ecs_config.access_key_id = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'] ecs_config.access_key_secret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] # Set the endpoint. ecs_config.endpoint = 'ecs-cn-hangzhou.aliyuncs.com' # Initialize the ECS client with the configuration and return it. return ecs_client.Client(ecs_config) if __name__ == '__main__': try: # Initialize the ECS client. client = init_ecs_client() # Create a DescribeRegionsRequest object. describeRegions_request = ecs_20140526_models.DescribeRegionsRequest() # Send a describeRegions request to get region information. response = client.describe_regions(describeRegions_request) # Print the response. print(response.body) except UnretryableException as e: # Handle network exceptions. print(e) except TeaException as e: # Handle service exceptions. print(e) except Exception as e: # Handle other exceptions. print(e) -
-
(Optional) Copy sample code from OpenAPI Portal into a file and run it. Auto-generate SDK sample code.