Tencent Cloud Common SDK for Python
The `tencentcloud-sdk-python-common` library provides the foundational components for interacting with Tencent Cloud services in Python. It includes core utilities for authentication, client profiles, and base models, serving as a crucial dependency for service-specific SDK modules like `tencentcloud-sdk-python-cvm`. It is part of the larger `tencentcloud-sdk-python` project, currently at version 3.1.79, with frequent updates to support new services and features.
Common errors
-
ModuleNotFoundError: No module named 'tencentcloud.cvm'
cause The service-specific package (e.g., CVM) was not installed, or only `tencentcloud-sdk-python-common` was installed without the specific service module.fixInstall the full SDK (`pip install tencentcloud-sdk-python`) or the specific service package (`pip install tencentcloud-sdk-python-cvm`). -
TencentCloudSDKException: Code=AuthFailure.SecretIdNotFound
cause The Tencent Cloud SecretId or SecretKey were not provided, are incorrect, or the environment variables are not loaded.fixEnsure `TENCENTCLOUD_SECRET_ID` and `TENCENTCLOUD_SECRET_KEY` environment variables are set correctly, or pass them directly when initializing `Credential(secret_id, secret_key)`. -
TypeError: 'module' object is not callable (e.g., client = CvmClient)
cause Attempting to use a class (like `CvmClient` or `DescribeInstancesRequest`) as if it were a function without instantiating it.fixEnsure you instantiate classes using parentheses, e.g., `client = CvmClient(cred, region, client_profile)` and `req = DescribeInstancesRequest()`. -
ModuleNotFoundError: No module named 'QcloudApi'
cause Attempting to use code from the legacy and deprecated `qcloud_sdk_python` package with the modern SDK.fixMigrate your code to use the `tencentcloud-sdk-python` library. Update imports and client initialization patterns to match the current SDK documentation.
Warnings
- gotcha The Tencent Cloud Python SDK has undergone a major rewrite. Ensure you are using the modern `tencentcloud-sdk-python` package family (v3+), not the deprecated `qcloud_sdk_python`.
- gotcha The SDK is modular. If you only install `tencentcloud-sdk-python-common`, you will not have access to service-specific clients (e.g., `CvmClient`) for API calls.
- gotcha Credential handling is critical. Incorrectly providing `SecretId` and `SecretKey` or omitting them is a common source of authentication failures.
Install
-
pip install tencentcloud-sdk-python-common -
pip install tencentcloud-sdk-python
Imports
- Credential
from tencentcloud.common.credential import Credential
- ClientProfile
from tencentcloud.common.profile.client_profile import ClientProfile
- HttpProfile
from tencentcloud.common.profile.http_profile import HttpProfile
- AbstractModel
from tencentcloud.common.abstract_model import AbstractModel
- TencentCloudSDKException
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
Quickstart
import os
from tencentcloud.common.credential import Credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cvm.v20170312.client import CvmClient
from tencentcloud.cvm.v20170312.models import DescribeInstancesRequest
try:
# Set credentials via environment variables for security
secret_id = os.environ.get("TENCENTCLOUD_SECRET_ID", "")
secret_key = os.environ.get("TENCENTCLOUD_SECRET_KEY", "")
region = "ap-guangzhou" # Replace with your desired region
cred = Credential(secret_id, secret_key)
http_profile = HttpProfile()
# Specify the endpoint for the service if needed, otherwise it's auto-resolved
http_profile.endpoint = "cvm.tencentcloudapi.com"
client_profile = ClientProfile()
client_profile.httpProfile = http_profile
# Initialize a service client (e.g., CVMClient) using common components
client = CvmClient(cred, region, client_profile)
# Create a request object for a specific API operation
req = DescribeInstancesRequest()
# For a basic test, no specific filters are applied, listing all instances
# Call the API and print the response
resp = client.DescribeInstances(req)
print(resp.to_json_string())
except TencentCloudSDKException as err:
print(f"An error occurred: {err}")
print(f"Error Code: {err.get_code()}")
print(f"Request ID: {err.get_request_id()}")