Help Center/ Data Warehouse Service/ SDK Reference/ Python SDK Calling Example
Updated on 2026-11-12 GMT+08:00

Python SDK Calling Example

This section uses the DWS API for querying flavor information (ListNodeTypes) as an example to describe how to use the Huawei Cloud Python SDK.

Prerequisites

  • You have obtained the access key (AK) and secret access key (SK) corresponding to your Huawei Cloud account. For details, see Creating Access Keys.
  • Python 3.6 or later has been installed in the environment where the SDK runs.

Installing the SDK

Run the commands below in the CLI to install the Huawei Cloud Python SDK. The Python SDK 3.1.217 is used as an example. For details about the latest SDK version, visit SDK Center.

  • Install the SDK package of a single cloud service.
    pip install huaweicloudsdkdws==3.1.217 
  • Install the SDK package of all cloud services.
    pip install huaweicloudsdkall==3.1.217

Using the SDK

The following is a complete Python example, which shows how to use the Huawei Cloud Python SDK to call the ListNodeTypes API.

  • To obtain the SDK example code for other APIs, see Obtaining SDK Sample Code.
  • This example provides only the basic API parameter settings. For more advanced settings, see the User Manual of the SDK.
  • There will be huge security risks if the AK and SK used for authentication are directly written into the code. You are advised to encrypt the AK and SK and store them in the configuration file or environment variables, and decrypt them when you use them. In this example, the AK and SK are stored as the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK. For details, see Environment Variables.
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkdws.v2.region.dws_region import DwsRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkdws.v2 import *

if __name__ == "__main__":
    # ==================== Initialize the client. ==================== 
    # Obtain the AK/SK from the environment variables. Before running, you need to set the AK/SK as environment variables.
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
 
    # Configure an authentication credential.
    credentials = BasicCredentials(ak, sk)
 
    # Initialize the client.
    client = DwsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(DwsRegion.value_of("af-north-1")) \
        .build()

    try:
        # ==================== Construct a request object. ==================== 
        # Use the request object provided by the SDK to encapsulate request parameters.
        request = ListNodeTypesRequest()
 
        # ==================== Initiate a request and handle the response. ==================== 
        # Use the client to call the corresponding API. If the API is successfully called, a response object is returned. If the API fails to be called, an exception is thrown.
        response = client.list_node_types(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)