Argos Translate
Argos Translate is an open-source neural machine translation (NMT) library for Python, based on OpenNMT's CTranslate2. It enables offline translation, supporting a wide array of languages through downloadable '.argosmodel' packages. The library can be utilized as a Python API, a command-line tool, or a GUI application. The current version is 1.11.0, with new releases typically published every three months.
Common errors
-
Package not found for translating from 'en' to 'es'.
cause The core `argostranslate` library is installed, but the specific language model package (e.g., English to Spanish) has not been downloaded and installed.fixYou must explicitly download and install language models. Use `argostranslate.package.update_package_index()` to get available packages, then filter and install the desired one using `argostranslate.package.install_from_path(package_to_install.download())`. Alternatively, use the `argospm` command-line tool: `argospm install translate-en_es`. -
'argos-translate' is not recognized as an internal or external command, operable program or batch file.
cause The `argos-translate` or `argos-translate-gui` executables are not in your system's PATH, or they are not correctly linked/aliased after installation.fixEnsure that the directory where `pip` installs executables (often `~/.local/bin` on Linux/macOS, or `Scripts` subdirectory in your Python installation on Windows) is included in your system's PATH environment variable. Using a Python virtual environment and activating it can help manage this. On Windows, you might need to invoke it via `python -m argostranslate.cli` or specify the full path to the script. -
FileNotFoundError: [WinError 2] The system cannot find the file specified
cause This error can occur during installation or execution, often related to underlying C++ dependencies (like `ctranslate2` needing a specific compiler or runtime, or `sentencepiece` build issues) or missing system-level tools.fixEnsure you have the necessary build tools for Python packages (e.g., Microsoft C++ Build Tools on Windows). If using a specific Python version (like 3.12), verify compatibility for `ctranslate2` and `sentencepiece`. A clean virtual environment can sometimes resolve conflicts.
Warnings
- breaking Major breaking changes occurred around versions 1.2 and 1.4, specifically deprecating `load_available_packages` for `get_available_packages` and `load_installed_languages` for `get_installed_languages`. Other changes affected `translate.apply_packaged_translation`, `ITranslate.split_into_paragraphs`, and `package.Package.remove`.
- gotcha Repeated calls to `argostranslate.translate.translate()` within a loop can be inefficient due to models potentially being reloaded. It's more performant to load the translation object once and reuse it.
- gotcha Installation issues on Python 3.12 have been reported, primarily due to underlying dependencies like `ctranslate2` or `sentencepiece` not having readily available wheels or requiring specific build environments for Python 3.12 at the time of the issue.
Install
-
pip install argostranslate
Imports
- package
from argostranslate import package
import argostranslate.package
- translate
from argostranslate import translate
import argostranslate.translate
Quickstart
import argostranslate.package
import argostranslate.translate
# Define source and target languages using ISO 639-1 codes
from_code = "en"
to_code = "es"
# Update package index and download/install the desired language model
print("Updating package index...")
argostranslate.package.update_package_index()
available_packages = argostranslate.package.get_available_packages()
package_to_install = next(
filter(
lambda x: x.from_code == from_code and x.to_code == to_code,
available_packages
),
None
)
if package_to_install:
print(f"Downloading and installing {from_code} -> {to_code} package...")
download_path = package_to_install.download()
argostranslate.package.install_from_path(download_path)
print("Package installed.")
else:
print(f"No package found for {from_code} -> {to_code}. Please check available packages or codes.")
# Perform translation
installed_languages = argostranslate.translate.get_installed_languages()
from_lang = next(filter(lambda x: x.code == from_code, installed_languages), None)
to_lang = next(filter(lambda x: x.code == to_code, installed_languages), None)
if from_lang and to_lang:
print(f"Translating 'Hello World' from {from_code} to {to_code}...")
translatedText = from_lang.get_translation(to_lang).translate("Hello World")
print(f"Translated text: {translatedText}")
else:
print("Source or target language not found among installed languages.")