Install PyCharm on Windows, create a Python project, and make your first Alibaba Cloud API call.
Before you begin
-
A computer running Windows.
-
Python installed on your system.
-
An AccessKey pair from a RAM user. Do not use your Alibaba Cloud account's AccessKey pair.
Install PyCharm and create a project
Step 1: Install PyCharm
-
Navigate to the official PyCharm download page.
-
Find the Community Edition and click Download.
-
Run the downloaded installer and follow the setup wizard.
ImportantIn the Installation Options step, select Add "bin" folder to the PATH to enable command-line access to PyCharm.
Step 2: Create a new project
-
Open PyCharm and click New Project.
-
In the New Project dialog box, configure your project:
-
Name: Enter a name for your project, such as
my-first-app. -
Location: The directory for your project files.
-
Interpreter type: The runtime environment for your project.
-
Project venv (Recommended): Creates an isolated virtual environment (
venv) for your project, preventing dependency conflicts between projects.Python version: Defaults to the Python version on your system. Select a different version if multiple are installed.
In the New Project dialog box, complete the following configuration:
-
Name: Enter a project name, for example
my-first-app. -
Location: Select the directory where the project is stored, for example
D:\ide\PythonProjects. -
Create a welcome script: Select this option if needed.
After the configuration is complete, click Create in the lower-right corner to create the project.
-
-
Base conda: Uses the Conda package manager. Suited for data science, scientific computing, and multi-language projects with complex dependencies.
-
Custom environment: Manually configure the environment.
-
Environment: Create a new environment or use an existing one.
-
Type: The environment tool.
Virtualenvis the default. -
Base python: The Python interpreter version for the environment.
-
Location: The directory for the environment.
-
Inherit packages from base interpreter: Includes packages from the base interpreter.
-
Make available to all projects: Makes this environment accessible to other projects.
In the New Project dialog box, select the Pure Python project type on the left, set Name (for example,
my-first-app) and Location (the directory where the project is saved) on the right, and then click Create.
-
-
-
-
-
Click Create.
Step 3: Create and run a Python file
-
In the Project tool window on the left, right-click your project's folder (such as
my-first-app) and choose New > Python File. -
In the dialog box, enter a name for your file, such as
hello_alibabacloud, and press Enter. -
In the editor window for
hello_alibabacloud.py, type the following code:print("Hello, Alibaba Cloud!") -
To run the script, click the green Run icon in the upper-right corner of the editor. The output
Hello, Alibaba Cloud!appears in the Run tool window at the bottom.
Use the integrated terminal
PyCharm's integrated terminal uses your project's virtual environment automatically. Click the Terminal tab or press Alt+F12 to open it. Use it to run scripts (python hello_alibabacloud.py) or install packages with pip (pip install requests).
Content of hello_alibabacloud.py:
print("Hello, Alibaba Cloud!")
In the terminal, run the script with the python command and install third-party packages with pip install:
(.venv) PS D:\ide\PythonProjects\my-first-app> python hello_alibabacloud.py
Hello, Alibaba Cloud!
(.venv) PS D:\ide\PythonProjects\my-first-app> pip install requests
Collecting requests
Obtaining dependency information for requests from https://files.pythonhosted.org/packages/70/8e/0e2d847013cb52cd35b38c009bb167a1a26b2ce6cd6965bf26b47bc0bf44/requests-2.31.0-py3-none-any.whl.metadata
Downloading requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)
Collecting charset-normalizer<4,>=2 (from requests)
Obtaining dependency information for charset-normalizer<4,>=2 from https://files.pythonhosted.org/packages/b6/7c/8debebb4f90174074b827c63242c23851bdf00a53249fba57fef3416e40/charset_normalizer-3.3.2-cp312-cp312-win_amd64.whl.metadata
...ding charset_normalizer-3.3.2-cp312-cp312-win_amd64.whl.metadata (34 kB)
Collecting idna<4,>=2.5 (from requests)
Obtaining dependency information for idna<4,>=2.5 from https://files.pythonhosted.org/packages/e5/3e/741d8c82801c347547f8a2a06a57dbb01992be9e948df2ea0eda2c8b79e8/idna-3.7-py3-none-any.whl.metadata
Downloading idna-3.7-py3-none-any.whl.metadata (9.9 kB)
What to do next
Start building applications that interact with Alibaba Cloud services.
Example 1: Call a service API with the Alibaba Cloud SDK
Use the Alibaba Cloud SDK for Python to call the ECS DescribeInstanceTypeFamilies operation.
Set up authentication
API calls require an AccessKey pair from a RAM user. For more information, see Create an AccessKey.
Always store credentials in environment variables, never in code. For instructions, see Configure environment variables in Linux, macOS, and Windows.
Generate and run the SDK sample code
-
Navigate to the SDK Center.
-
Select the service (such as ECS) and the API operation (such as DescribeInstanceTypeFamilies).
-
On the Parameters tab, fill in the required parameters. For this example, set
RegionIdtocn-qingdaoandGenerationtoecs-5. -
In the rightmost column, click the SDK Sample Code tab, select Python, and click Download Project.
In the SDK sample area on the right, set the SDK generation to V2.0 Recommended and the language to Python.
-
Decompress the downloaded package and open the project in PyCharm.
-
After you run the code, a
statusCodevalue of 200 in the response indicates that the API call was successful, and the response body contains the list of ECS instance type families. The following is part of the response:: '1rHpYSAeV5ewkgoDCes4x8A7'}, 'statusCode': 200, 'body': {'InstanceTypeFamilies': {'InstanceTypeFamily': [{'Generation': 'ecs-5', 'InstanceTypeFamilyId': 'ecs.g6'}, {'Generation': 'ecs-5', 'InstanceTypeFamilyId': 'ecs.ebmg
For more information, see Use the Alibaba Cloud Python SDK in an IDE.
Example 2: Call a large language model (Qwen)
Call the Qwen large language model (LLM) through the OpenAI-compatible API from Alibaba Cloud Model Studio.
Set up authentication
Activate Model Studio and get an API key. For more information, see Account setup.
Configure your API key as an environment variable named DASHSCOPE_API_KEY. For instructions, see Set your API key as an environment variable.
Install the SDK and run the sample code
-
In the PyCharm terminal, install the OpenAI Python SDK:
pip install -U openaiDownloading jiter-0.8.2-cp312-cp312-win_amd64.whl (204 kB) 204.7/204.7 kB 654.9 kB/s eta 0:00:00 Downloading pydantic-2.10.6-py3-none-any.whl (431 kB) 431.7/431.7 kB 642.5 kB/s eta 0:00:00 Downloading pydantic_core-2.27.2-cp312-cp312-win_amd64.whl (2.0 MB) 2.0/2.0 MB 715.4 kB/s eta 0:00:00 Downloading sniffio-1.3.1-py3-none-any.whl (10 kB) Downloading tqdm-4.67.1-py3-none-any.whl (78 kB) 78.5/78.5 kB 875.6 kB/s eta 0:00:00 Downloading typing_extensions-4.12.2-py3-none-any.whl (37 kB) Downloading annotated_types-0.7.0-py3-none-any.whl (13 kB) Downloading h11-0.14.0-py3-none-any.whl (58 kB) 58.3/58.3 kB 762.2 kB/s eta 0:00:00 Installing collected packages: typing-extensions, tqdm, sniffio, jiter, h11, distro, annotated-types, pydantic-core, httpcore, anyio, pydantic, httpx, openai Successfully installed annotated-types-0.7.0 anyio-4.8.0 distro-1.9.0 h11-0.14.0 httpcore-1.0.7 httpx-0.28.1 jiter-0.8.2 openai-1.65.2 pydantic-2.10.6 pydantic-core-2.27.2 sniffio-1.3.1 tqdm-4.67.1 typing-extensions-4.12.2A
Successfully installed ... openai-x.x.xmessage confirms the installation. -
Create a new Python file and add the following code:
import os from openai import OpenAI try: client = OpenAI( # Reads the API key from the DASHSCOPE_API_KEY environment variable. api_key=os.getenv("DASHSCOPE_API_KEY"), base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", ) completion = client.chat.completions.create( model="qwen-plus", messages=[ {'role': 'system', 'content': 'You are a helpful assistant.'}, {'role': 'user', 'content': 'Who are you?'} ] ) print(completion.choices[0].message.content) except Exception as e: print(f"An error occurred: {e}") -
Run the script. You should see a response from the Qwen model.
D:\official_site\Demo\venv\Scripts\python.exe D:\official_site\Demo\venv\ecsram\TongYi.py I am a large language model from Alibaba Cloud. My name is Qwen.
For more information, see the OpenAI Python SDK.