Python SDK
The Machine Translation Python SDK lets you translate text programmatically. Two API variants are available: the dedicated version (optimized for e-commerce content) and the general version (for all other content).
SDK repository: aliyun-python-sdk-alimt on GitHub
Core library: aliyun-python-sdk-core on PyPI
Prerequisites
Before you begin, make sure you have:
Python 2.6.x, 2.7.x, 3.x, and later
An Alibaba Cloud account with real-name verification completed
An AccessKey pair (AccessKey ID and AccessKey secret)
Install the SDK
Install the SDK using pip (recommended):
pip install alibabacloud_alimt20181012==1.1.0Note: For Python 3.x, installaliyun-python-sdk-core-v3instead ofaliyun-python-sdk-corewhen installing the core library from source.
To install from source instead, clone the repository and run:
git clone https://github.com/aliyun/aliyun-openapi-python-sdk.git
cd aliyun-openapi-python-sdk/aliyun-python-sdk-core
python setup.py install
cd ../aliyun-python-sdk-alimt
python setup.py installGet an AccessKey pair
An AccessKey pair identifies your Alibaba Cloud account when making API requests. It consists of an AccessKey ID and an AccessKey secret.
Log on to the RAM console.
Create an AccessKey pair or select an existing one.
Make sure the AccessKey pair is in the Enabled state.
Store your credentials in environment variables rather than hard-coding them in source code:
export ALIBABA_CLOUD_ACCESS_KEY_ID=<your-access-key-id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<your-access-key-secret>Make a translation call
All examples use AcsClient to initialize a client and do_action_with_exception to send requests. The response is a JSON-encoded byte string — parse it with json.loads() to access the result.
Dedicated version
Use the dedicated version (TranslateRequest) for e-commerce content. It accepts a scene parameter that optimizes translation for specific content types.
Create a file named
translate_dedicated.pywith the following code:#coding: utf-8 import json import os from aliyunsdkcore.client import AcsClient from aliyunsdkalimt.request.v20181012 import TranslateRequest # Load credentials from environment variables client = AcsClient( os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), "cn-hangzhou" ) # Build the translation request request = TranslateRequest.TranslateRequest() request.set_SourceLanguage("en") request.set_TargetLanguage("zh") request.set_SourceText("hello") request.set_FormatType("text") request.set_Scene("title") # Valid values: title, description, communication request.set_method("POST") # Send the request and print the response response = client.do_action_with_exception(request) print(json.loads(response))Run the file:
python translate_dedicated.py
The scene parameter accepts the following values:
| Value | Content type |
|---|---|
title | Product title |
description | Product description |
communication | Product communication |
General version
Use the general version (TranslateGeneralRequest) for everyday translation without content-type optimization.
Create a file named
translate_general.pywith the following code:#coding: utf-8 import json import os from aliyunsdkcore.client import AcsClient from aliyunsdkalimt.request.v20181012 import TranslateGeneralRequest # Load credentials from environment variables client = AcsClient( os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), "cn-hangzhou" ) # Build the translation request request = TranslateGeneralRequest.TranslateGeneralRequest() request.set_SourceLanguage("en") request.set_TargetLanguage("zh") request.set_SourceText("hello") request.set_FormatType("text") request.set_method("POST") # Send the request and print the response response = client.do_action_with_exception(request) print(json.loads(response))Run the file:
python translate_general.py
Parameters
| Parameter | Method | Description | Example |
|---|---|---|---|
| Source language | set_SourceLanguage() | Language code of the input text | "en" |
| Target language | set_TargetLanguage() | Language code to translate into | "zh" |
| Source text | set_SourceText() | Text to translate | "hello" |
| Format type | set_FormatType() | Input format | "text" |
| Scene | set_Scene() | Content type optimization (dedicated version only). Valid values: title, description, communication | "title" |