DeepL Python Library

1.30.0 · active · verified Mon Apr 13

The DeepL Python Library is an official client for the DeepL API, providing a convenient way for Python applications to interact with DeepL's language AI for high-quality text and document translation. It supports all major API functions and is actively maintained with frequent releases, currently at version 1.30.0.

Warnings

Install

Imports

Quickstart

Initializes the DeepLClient with an API key, preferably from an environment variable, and performs a basic text translation. Note the `server_url` option for DeepL API Free users.

import os
import deepl

auth_key = os.environ.get("DEEPL_AUTH_KEY", "") # Replace with your key or set DEEPL_AUTH_KEY environment variable
if not auth_key:
    print("Please set the DEEPL_AUTH_KEY environment variable.")
    exit(1)

# For DeepL API Free, use server_url="https://api-free.deepl.com"
deepL_client = deepl.DeepLClient(auth_key)

text_to_translate = "Hello, world!"
target_language = "FR"

try:
    result = deepL_client.translate_text(text_to_translate, target_lang=target_language)
    print(f"Translated text: {result.text}")
    print(f"Detected source language: {result.detected_source_lang}")
except deepl.exceptions.DeepLException as e:
    print(f"Error during translation: {e}")

view raw JSON →