Tencent Cloud SDK for Python
Tencent Cloud SDK for Python is the official software development kit for interacting with Tencent Cloud services. It provides a unified, Pythonic interface to TencentCloud API 3.0, enabling developers to easily integrate various cloud products like CVM, VPC, and CBS into their applications. The SDK supports Python 3.6 and above, offering consistent API usage, error codes, and response formats across different services. It is actively maintained with frequent updates to support new services and features.
Warnings
- gotcha On macOS, Python 3.6+ users may encounter SSL certificate verification errors. This is due to Python no longer using system certificates. You typically need to run `/Applications/Python 3.6/Install Certificates.command` (adjust path for your Python version) to install `certifi`'s certificates.
- gotcha Do not mix the full SDK package (`tencentcloud-sdk-python`) with individual product packages (e.g., `tencentcloud-sdk-python-cvm`) in the same environment. This can lead to conflicts and unexpected behavior.
- gotcha If operating behind a proxy, you must explicitly set the `https_proxy` system environment variable, otherwise, connection timeout exceptions may occur.
- breaking The default language of the SDK has been changed to `en-US` in recent versions. If your application relies on localized responses, this change might affect the output of SDK calls.
Install
-
pip install tencentcloud-sdk-python -
pip install --upgrade tencentcloud-sdk-python-common tencentcloud-sdk-python-cvm -
pip install -i https://mirrors.tencent.com/pypi/simple/ --upgrade tencentcloud-sdk-python
Imports
- Credential
from tencentcloud.common import credential
- TencentCloudSDKException
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
- CvmClient, models
from tencentcloud.cvm.v20170312 import cvm_client, models
Quickstart
import os
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cvm.v20170312 import cvm_client, models
try:
# Get credentials from environment variables (recommended for security)
secret_id = os.environ.get('TENCENTCLOUD_SECRET_ID', 'YOUR_SECRET_ID')
secret_key = os.environ.get('TENCENTCLOUD_SECRET_KEY', 'YOUR_SECRET_KEY')
# Check if credentials are placeholders
if secret_id == 'YOUR_SECRET_ID' or secret_key == 'YOUR_SECRET_KEY':
print("Please set TENCENTCLOUD_SECRET_ID and TENCENTCLOUD_SECRET_KEY environment variables.")
exit(1)
cred = credential.Credential(secret_id, secret_key)
# Instantiate the client object for a specific product (e.g., CVM)
# The second parameter is the region, e.g., 'ap-shanghai' or 'ap-guangzhou'
client = cvm_client.CvmClient(cred, "ap-shanghai")
# Instantiate a request object for the API (e.g., DescribeInstancesRequest)
req = models.DescribeInstancesRequest()
# Call the API method through the client object
resp = client.DescribeInstances(req)
# Print the returned response in JSON format
print(resp.to_json_string())
except TencentCloudSDKException as err:
print(f"An error occurred: {err}")
except Exception as e:
print(f"An unexpected error occurred: {e}")