All Products
Search
Document Center

Alibaba Cloud SDK:Get started with Alibaba Cloud Darabonba SDK for Python

Last Updated:Jan 10, 2024

This topic describes how to install the environment for using Alibaba Cloud Darabonba SDK for Python and run the sample code to call an API operation.

Preparations

Prerequisites

  • Python 3 is downloaded and installed. To download Python 3, go to the Downloads page on the Python official website.

  • An integrated development environment (IDE) is downloaded and installed. In this topic, PyCharm is used. To download PyCharm, visit the PyCharm official website.

  • An AccessKey pair is created. When you call an API operation, you must use an AccessKey pair to complete identity authentication. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to create an AccessKey pair, see Create an AccessKey pair.

Python version

Run the python --version command on the terminal to check the Python version.

Run the sample code

Step 1: Download an SDK project

  1. Log on to OpenAPI Explorer.

  2. In the top navigation bar, click Select a cloud service. In the panel that appears, select the cloud service that you want to manage. The page of the selected cloud service appears.

  3. In the top navigation bar, click API Debugging. Then, a page with three columns appears.

    image.png

  4. Select the API operation that you want to call in the leftmost column. Configure the required parameters in the middle column. Then, click the SDK Sample Code tab in the rightmost column.

  5. Select V2.0 from the SDK Version drop-down list. Select a programming language based on your business requirements.

  6. Click Download Project to download the sample code.

  7. If you want to generate generic code, turn on Common Request. For more information, see Generic calls and specialized calls.

Step 2: Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

  • If you use a Linux or macOS operating system, perform the following operations:

    • Create the .bash_profile file.

      Note

      If you configure environment variables for the first time, you can run the touch ~/.bash_profile command to create the configuration file. If the configuration file already exists, run the vim ~/.bash_profile command.

      touch .bash_profile
      vim ~/.bash_profile
    • Configure the .bash_profile file.

      # Replace <ACCESS_KEY_ID> and <ACCESS_KEY_SECRET> in the following commands with your AccessKey ID and AccessKey secret. 
      export ALIBABA_CLOUD_ACCESS_KEY_ID=<ACCESS_KEY_ID>
      export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET>
    • Use the .bash_profile file.

      # Run the commands in the configuration file.
      source ~/.bash_profile
      # Check whether the configuration takes effect.
      echo $ALIBABA_CLOUD_ACCESS_KEY_ID
  • If you use a Windows operating system, create a file to add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, and set the variables to your AccessKey ID and AccessKey secret.

Restart the IDE. Otherwise, the environment variables may fail to be read.

Step 3: Run the code

  1. Run the code in PyCharm and view the returned information, including the request ID, on the terminal.

    Note

    If a ModuleNotFoundError error occurs, run the which python command on the terminal to check whether the interpreter for the current environment is used. If not, press Shift+Command+P on macOS or Ctrl+Shift+P on Windows or Linux, enter >Python: Select Interpreter, select the interpreter for the current environment, and then perform Step 2: Configure environment variables again.

  2. If you call an API operation to create an instance, the instance ID is also returned. You can log on to the console of the Alibaba Cloud service to verify the returned information based on the instance ID.