Registry / communication / googletrans

googletrans

JSON →
library4.0.2pypypi✓ verified 52d ago

Googletrans is a free and unlimited Python library that implements the Google Translate API. It leverages the Google Translate Ajax API for language detection and text translation. As of version 4.0.2, the library features a modern async-only API, support for bulk translations, automatic language detection, and proxy configurations. It is compatible with Python 3.8+ and is actively maintained.

communicationhttp-networkinggcp
pip install googletrans
Install & Compatibility
Where this runs
tested against v4.0.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.388s · 23.3MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 2.4s · import 0.324s · 24MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Translator
from googletrans import Translator

Initializes a Translator instance to translate a single string, multiple strings in a batch, and detect the language of a given text. Note that the API is now async-only.

import asyncio from googletrans import Translator async def translate_text(): translator = Translator() text_to_translate = "Hello, how are you?" # Translate a single text translation = await translator.translate(text_to_translate, dest='es') print(f"Original: {translation.origin}, Translated (ES): {translation.text}") # Translate multiple texts texts = ["The quick brown fox", "jumps over", "the lazy dog"] translations = await translator.translate(texts, dest='fr') for t in translations: print(f"Original: {t.origin} -> Translated (FR): {t.text}") # Detect language detection = await translator.detect("Bonjour") print(f"Detected language: {detection.lang} with confidence {detection.confidence}") if __name__ == "__main__": asyncio.run(translate_text())
Debug
Known issues
breakingVersion 4.0.0 introduced an async-only API. All synchronous translation and detection methods have been removed. Existing code using synchronous calls will break.
fix
Rewrite code to use `await` with `async/await` syntax and run within an `asyncio` event loop. For example, `translator.translate('text')` becomes `await translator.translate('text')`.
affects: >=4.0.0
gotchaMany older tutorials and Stack Overflow answers still recommend installing `googletrans==4.0.0rc1`. This is an outdated pre-release version and may lead to unexpected behavior, bugs, or missing features compared to the latest stable release.
fix
Always install the latest stable version using `pip install googletrans`. If you encounter issues, consider uninstalling `googletrans` and then reinstalling the stable version.
affects: <4.0.0 (rc1 specifically)
gotchaGoogletrans is an unofficial library that relies on the public Google Translate web API. Google frequently updates its web services, which can occasionally cause the library to stop working or return HTTP 5xx errors (e.g., due to IP bans or API changes).
fix
Implement robust error handling, rate limiting, and retry mechanisms. Consider using `service_urls` parameter to rotate through different Google Translate domains or use proxies. For critical applications requiring high stability and rate limits, consider Google's official Cloud Translation API.
affects: All versions
gotchaMaking too many requests in a short period can lead to temporary IP bans or rate limiting from Google, resulting in connection errors or HTTP 5xx status codes.
fix
Introduce delays between requests, especially in loops or batch processes (e.g., `time.sleep(1)` or more). Implement exponential backoff for retries. Consider using proxies if making a very high volume of requests.
affects: All versions
gotchaThe Google Translate web API (and thus googletrans) has a maximum character limit of approximately 15,000 characters per single translation request.
fix
For longer texts, split them into smaller chunks and translate them individually, then reassemble the translated parts.
affects: All versions
Errors
Common errors & fixes
AttributeError: 'NoneType' object has no attribute 'group'
This error typically occurs when the `googletrans` library fails to parse the response from the Google Translate API, often due to changes in Google's internal API structure, leading to an inability to extract the translation data.
fix
Explicitly define `service_urls` when initializing the `Translator` object, for example: `translator = Translator(service_urls=['translate.googleapis.com'])`. For some users, installing a specific pre-release like `pip install googletrans==4.0.0rc1` or `pip install googletrans==3.1.0a0` has also resolved it.
ModuleNotFoundError: No module named 'googletrans'
The `googletrans` package is not installed in the Python environment currently in use, or the Python interpreter cannot locate the installed package.
fix
Install the library using pip: `pip install googletrans`. Ensure you are installing it into the correct Python environment if you are using virtual environments or multiple Python installations.
AttributeError: 'Translator' object has no attribute 'translate'
With `googletrans` version 4.0.0 and later (including 4.0.2), the library transitioned to an asynchronous-only API. This error occurs when attempting to call `translate` synchronously without using `await` inside an `async` function.
fix
Rewrite your translation code to use Python's `async` and `await` syntax. Define an `async` function, instantiate the `Translator` within an `async with` block, and `await` the `translate` call.

```python
import asyncio
from googletrans import Translator

async def translate_text(text, dest_lang='en'):
    async with Translator() as translator:
        result = await translator.translate(text, dest=dest_lang)
        return result.text

# Example usage:
# translated_string = asyncio.run(translate_text('안녕하세요.'))
# print(translated_string)
```
Exception: Unexpected status code "429" from ['translate.google.com']
This error, or similar HTTP 5xx errors, indicates that your IP address has been temporarily banned or rate-limited by Google due to sending too many requests in a short period, as `googletrans` uses the unofficial web API.
fix
To mitigate rate limiting, introduce delays between translation requests, consider using a proxy, or provide multiple service URLs to the `Translator` constructor to distribute requests.

```python
from googletrans import Translator
import time

translator = Translator(service_urls=[
  'translate.google.com',
  'translate.google.co.kr',
  'translate.google.cn'
])

def translate_with_delay(text, dest_lang='en'):
    # In an async context, you would use await asyncio.sleep(delay)
    time.sleep(1) # Add a delay between requests
    result = translator.translate(text, dest=dest_lang)
    return result.text

# Note: For async API (googletrans 4.0.2), this synchronous example
# would need to be adapted to use async/await with an async sleep.
```
Upgrade
Version history
4.0.2latest on PyPI
Audit
Dependencies
httpxrequiredCore HTTP client for making requests to the Google Translate API.
hyperoptionalOptional dependency for improved performance via HTTP/2 support.
Agent activity
20 hits · last 30 days
node
6
claudebot
4
seranking-bot
4
ahrefsbot
3
amazonbot
1
bytedance
1
Resources