All Products
Search
Document Center

Alibaba Cloud SDK:Use the Alibaba Cloud Python SDK in an IDE

Last Updated:Jun 02, 2026

Learn how to use the Alibaba Cloud Python SDK with PyCharm on Windows.

Prerequisites

Use the SDK

Use the sample project from OpenAPI Portal

Note

If you cannot download the sample project from OpenAPI Portal, follow Use the SDK in an existing project.

  1. 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.

    1.png

  2. 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.

    2.png

  3. 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.

    image

  4. 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.

    image

    Note

    If dependencies fail to download, run python3 setup.py install in the terminal.

  5. 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.

    Important

    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.

  6. 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
    

    image

  7. View the result. In the console at the bottom, press Ctrl+F and search for statusCode. If "statusCode": 200 is displayed, the call is successful.

    image

Use the SDK in an existing project

  1. 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.

    image

  2. Install the SDK.

    In PyCharm, press Alt+F12 to open the terminal. Paste the installation command and press Enter.

    image

  3. 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.

  4. Initialize the client.

    Initialize the ECS client before calling an API.

    Important
    1. You must use an AccessKey for identity verification when you initialize the client. For information about how to obtain an AccessKey, see Create an AccessKey.

    2. 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.

    3. 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()
    
  5. Call the API. Review the API Documentation for the target API. The following example calls the `DescribeRegions` API of ECS.

    Note

    Each 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)
    
  6. Handle exceptions.

    The Python V2.0 SDK handles exceptions through `Tea.exceptions` with two exception types:

    1. `UnretryableException`: Thrown when the maximum number of retries is reached due to network issues.

    2. `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)
    
  7. (Optional) Copy sample code from OpenAPI Portal into a file and run it. Auto-generate SDK sample code.

References

Advanced documentation