Help Center> SDK Developer Guide> Developer Guide> Python> Getting Started with Python SDK

Getting Started with Python SDK

Welcome to use HUAWEI CLOUD developer tools (Python SDK). Python SDK allows you to easily access cloud services using codes.

This tutorial describes how to install and use Python SDK and provides examples to help you quickly get started.

The supported Python SDK is developed based on the Python OpenStack SDK.

Prerequisites

  1. You have obtained a cloud platform account and provisioned all required services.
  2. You have installed Python, pip, and git. Python SDK is applicable to Python 2.7.10 to 2.7.15 and 3.4 to 3.7.

SDK Acquisition and Installation

If the pip is used for installation, run the following command:

pip install huaweicloud-sdk-python

How to Use

Set parameters, create connections, and invoke the SDK to access the service API. For details about the parameters, see Table 1.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding:utf-8 -*-
from openstack import connection

# create connection
username = "replace-with-your-username" # username
password = "replace-with-your-password" # user password
projectId = "replace-with-your-projectId"    # project ID
userDomainId = "replace-with-your-domainId"  # account ID
auth_url = "https://iam.example.com/v3"    # endpoint url
conn = connection.Connection(auth_url=auth_url,
                             user_domain_id=userDomainId,
                             project_id=projectId,
                             username=username,
                             password=password)

# set parameters
limit = 5

# define function for listing servers
def list_servers():
    # get server list with params
    servers = conn.compute.servers(limit=limit)
    # iterate servers list
    for server in servers:
        print(server)

# visit API
list_servers()
Table 1 Parameter description

Parameter

Description

Example Value

auth_url

Specifies the endpoint of the IAM service.

example in the https://iam.example.com/v3, indicates the Region.Cloud platform domain name. For details about the parameter, see here.

https://iam.cn-north-1.myhuaweicloud.com/v3

username

Specifies the IAM username. For details about how to obtain the username, see How Do I Obtain the IAM Username, Account ID, and Project ID?.

N/A

password

Specifies the IAM user password.

N/A

projectId

Specifies the project ID. For details about how to obtain the project ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?.

N/A

userDomainId

Specifies the account ID. For details about how to obtain the account ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?.

N/A