Alibaba Cloud Machine Translation SDK for Python
raw JSON → 3.2.0 verified Mon Apr 27 auth: no python
The Alibaba Cloud Machine Translation (alimt) SDK for Python, part of the Aliyun Python SDK family. Version 3.2.0 provides APIs for text translation, image translation, and document translation. Release cadence is irregular, tied to API updates.
pip install aliyun-python-sdk-alimt Common errors
error ModuleNotFoundError: No module named 'aliyunsdkcore' ↓
cause The required core library is missing.
fix
pip install aliyun-python-sdk-core
error aliyunsdkcore.acs_exceptions.error_code: InvalidAccessKeyId.NotFound ↓
cause The provided AccessKey ID or Secret is incorrect or the environment variables are not set.
fix
Ensure ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly.
error TypeError: unsupported operand type(s) for /: 'str' and 'str' ↓
cause Passing a string to SourceText when using SDK v3.0 incorrectly. The set_SourceText method expects a string correctly.
fix
This error typically occurs if you use the request incorrectly. Ensure you are calling set_SourceText with a plain string, not a dict or list.
Warnings
breaking In v3, the API version suffix changed from v20181012 to v20181012 (no change). However, the package structure was reorganized. If you were using v1 (aliyun-python-sdk-alimt<2.0), you must update imports from 'alimt' to 'aliyunsdkalimt'. ↓
fix Use from aliyunsdkalimt.request.v20181012 import * and install aliyun-python-sdk-core-v3.
gotcha The core library aliyun-python-sdk-core must be installed separately; it does not come as a dependency automatically. Use pip install aliyun-python-sdk-core. ↓
fix pip install aliyun-python-sdk-core
deprecated Attribute 'set_*' methods (like set_FormatType) are being deprecated in favor of direct properties in future versions. Currently both work. ↓
fix Use request.FormatType = 'text' instead of request.set_FormatType('text').
gotcha The API endpoint defaults to 'mt.cn-hangzhou.aliyuncs.com'. If your Region is not cn-hangzhou, you must set the endpoint region explicitly via client.set_region_id('your-region') or AcsClient's third argument. ↓
fix Pass the correct region to AcsClient (e.g., 'ap-southeast-1').
Imports
- Alimt wrong
from aliyun_sdk_alimt import Alimt import alimtcorrectfrom aliyunsdkalimt.request.v20181012 import TranslateGeneralRequest from aliyunsdkcore.client import AcsClient
Quickstart
import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkalimt.request.v20181012 import TranslateGeneralRequest
client = AcsClient(
os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', ''),
os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', ''),
'cn-hangzhou'
)
request = TranslateGeneralRequest()
request.set_FormatType('text')
request.set_SourceLanguage('en')
request.set_TargetLanguage('zh')
request.set_SourceText('Hello')
response = client.do_action_with_exception(request)
print(response.decode())