Tencent Cloud TMT SDK

raw JSON →
3.1.70 verified Sat May 09 auth: no python

Official Tencent Cloud SDK for the Machine Translation (TMT) service. Version 3.1.70. Released frequently alongside the main tencentcloud-sdk-python.

pip install tencentcloud-sdk-python-tmt
error ModuleNotFoundError: No module named 'tencentcloud'
cause The common base package (tencentcloud-sdk-python-common) is not installed or missing.
fix
Install the common SDK: pip install tencentcloud-sdk-python-common
error AttributeError: module 'tencentcloud.tmt' has no attribute 'TmtClient'
cause Wrong import path without the version subpackage.
fix
Use 'from tencentcloud.tmt.v20180321 import tmt_client' instead.
error TencentCloudSDKException: [InvalidParameter] invalid request parameter
cause ProjectId missing or set to an invalid value.
fix
Set req.ProjectId = 0 (or a valid project ID) in the request.
error TencentCloudSDKException: [AuthFailure] SecretId not found
cause Credentials not provided or incorrect.
fix
Check that TENCENTCLOUD_SECRET_ID and TENCENTCLOUD_SECRET_KEY are set correctly.
breaking Credentials: SecretId and SecretKey are required. Do NOT pass empty strings.
fix Set TENCENTCLOUD_SECRET_ID and TENCENTCLOUD_SECRET_KEY environment variables or use a proper credential provider.
gotcha Region must be explicitly set when creating the client, even if using default region via environment variable.
fix Pass a valid region string (e.g., 'ap-guangzhou') as the second argument to TmtClient.
gotcha ProjectId is required in TextTranslateRequest; use 0 if you have no project.
fix Add 'req.ProjectId = 0' or set to a valid project ID.
breaking The API version v20180321 is the only supported version. Do not attempt to use other version numbers.
fix Always import from tencentcloud.tmt.v20180321.

Translate 'Hello' from English to Chinese.

import os
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.tmt.v20180321 import tmt_client, models

try:
    cred = credential.Credential(
        secret_id=os.environ.get('TENCENTCLOUD_SECRET_ID', ''),
        secret_key=os.environ.get('TENCENTCLOUD_SECRET_KEY', '')
    )
    client = tmt_client.TmtClient(cred, 'ap-guangzhou')
    req = models.TextTranslateRequest()
    req.Source = 'en'
    req.Target = 'zh'
    req.SourceText = 'Hello'
    req.ProjectId = 0
    resp = client.TextTranslate(req)
    print(resp.TargetText)
except TencentCloudSDKException as e:
    print(e)