All Products
Search
Document Center

Python SDK

Last Updated: Jun 14, 2019

Download URL

GitHub address of the Machine Translation SDK:

https://github.com/aliyun/aliyun-openapi-python-sdk/tree/master/aliyun-python-sdk-alimt,You also need to download Alibaba Cloud aliyun-python-sdk-core

Installation Notes

Alibaba Cloud Python SDK supports Python 2.6.x, 2.7.x and 3.x and above, and provides both pip and GitHub installation methods.

  • Use pip to install (recommended)
  1. # Execute the following command to install the SDK through pip.
  2. pip install aliyun-python-sdk-core # Install Alibaba Cloud SDK core library
  3. pip install aliyun-python-sdk-alimt # Install Alibaba Cloud alimt SDK

If you are using python3.x, change pip install aliyun-python-sdk-core to pip install aliyun-python-sdk-core-v3.

  • Download GitHub source code

Install the Python SDK via GitHub.

  1. git clone https://github.com/aliyun/aliyun-openapi-python-sdk.git
  2. # Install Alibaba Cloud SDK core library
  3. cd aliyun-python-sdk-core
  4. python setup.py install
  5. # Install Alibaba Cloud alimt SDK
  6. cd aliyun-python-sdk-alimt
  7. python setup.py install

Procedure

To quickly start using the Machine Translation Python SDK, take the following steps:

Step 1. Create an Alibaba Cloud account

For more information about how to create an Alibaba Cloud account, see Sign up with Alibaba Cloud.

We recommend that you complete real-name authentication as soon as possible to better use Alibaba Cloud services. Otherwise, some services may be unavailable.

Step 2. Obtain an Alibaba Cloud Access Key

You must apply for an Alibaba Cloud Access Key for using the Machine Translation Python SDK.

Log on to Alibaba Cloud’s Access Key Management page. Select an Access Key (consisting of an Access Key ID and Access Key Secret) for the SDK. If you do not have an Access Key, create one and make sure the Access Key is enabled.

The Access Key will be used in the following steps and must be kept confidential.

Step 3. Start a Python program

Now, you can start using the Python SDK. Use any text editor or the Python IDE to run the following sample code, and you can interact with the Machine Translation server and obtain the relevant output.

Demo call example for the e-commerce edition

  1. #coding:utf-8
  2. from aliyunsdkcore.client import AcsClient
  3. from aliyunsdkcore.acs_exception.exceptions import ClientException
  4. from aliyunsdkcore.acs_exception.exceptions import ServerException
  5. from aliyunsdkalimt.request.v20181012 import TranslateECommerceRequest
  6. client = AcsClient(
  7. "<your-access-key-id>", // Alibaba Cloud Account AccessKey ID
  8. "<your-access-key-secret>",//Alibaba Cloud Account Access Key Secret
  9. "cn-hangzhou" // Region
  10. );
  11. # Create API requests and set parameters
  12. request = TranslateECommerceRequest.TranslateECommerceRequest()
  13. request.set_SourceLanguage("en") // Source language
  14. request.set_Scene("title") //Setting up the scene,Product title:title,product description:description,Product communication:communication
  15. request.set_SourceText("hello") // Source Text
  16. request.set_FormatType("text") //Format of translated text
  17. request.set_TargetLanguage("zh") //Target language
  18. # Initiate an API request and display the return value
  19. response = client.do_action_with_exception(request)
  20. print response

Demo call example for the common edition

  1. #coding:utf-8
  2. from aliyunsdkcore.client import AcsClient
  3. from aliyunsdkcore.acs_exception.exceptions import ClientException
  4. from aliyunsdkcore.acs_exception.exceptions import ServerException
  5. from aliyunsdkalimt.request.v20181012 import TranslateGeneralRequest
  6. client = AcsClient(
  7. "<your-access-key-id>", // Alibaba Cloud Account AccessKey ID
  8. "<your-access-key-secret>",// Alibaba Cloud Account Access Key Secret
  9. "cn-hangzhou" // Region
  10. );
  11. # Create API requests and set parameters
  12. request = TranslateGeneralRequest.TranslateGeneralRequest()
  13. request.set_SourceLanguage("en")
  14. request.set_SourceText("hello")
  15. request.set_FormatType("text")
  16. request.set_TargetLanguage("zh")
  17. # Initiate an API request and display the return value
  18. response = client.do_action_with_exception(request)
  19. print response