PyEnchant
PyEnchant is a Python library providing bindings for the Enchant spellchecking system. It allows developers to check the spelling of words, suggest corrections, and manage dictionaries for various languages. The current version is 3.3.0, and the library maintains an active release cadence, with updates addressing Python version compatibility and bug fixes.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes, including dropping support for Python 2. On macOS, the Enchant C library is no longer embedded in the wheel and must be installed separately via Homebrew or MacPorts. It also uncoupled PyEnchant's version from the Enchant C library's version and fixed compatibility issues with Enchant C library versions 2.0 and above.
- breaking The return type of `enchant.get_enchant_version()` changed from `bytes` to `str` in version 3.1.0.
- gotcha PyEnchant requires the underlying Enchant C library to be installed on your system. It is NOT installed automatically by `pip install pyenchant` on Linux or macOS. On Windows, pre-built binary wheels might include a basic Enchant C library, but for full functionality or specific configurations (e.g., `--no-binary`), manual installation is required.
- gotcha After installing PyEnchant and the Enchant C library, you also need to install language-specific dictionaries (e.g., for `en_US`, `de_DE`) which are typically provided as separate system packages (e.g., `hunspell-en-us`). Without these, PyEnchant will raise a `DictNotFoundError`.
- gotcha On Windows, PyEnchant version 3.1.1 changed its behavior from using `SetDllDirectory` to manipulating the `PATH` environment variable when loading the Enchant C library. This was to improve coexistence with other libraries. If you encounter DLL loading issues, especially in complex environments, this change might be relevant.
Install
-
pip install pyenchant
Imports
- enchant
import enchant
Quickstart
import enchant
try:
# Initialize a dictionary for American English
# Replace 'en_US' with your desired language tag
# Ensure the corresponding Enchant dictionaries are installed on your system
d = enchant.Dict("en_US")
# Check if a word is spelled correctly
word_correct = "hello"
print(f"'{word_correct}' is spelled correctly: {d.check(word_correct)}")
word_incorrect = "helllo"
print(f"'{word_incorrect}' is spelled correctly: {d.check(word_incorrect)}")
# Get suggestions for a misspelled word
suggestions = d.suggest(word_incorrect)
print(f"Suggestions for '{word_incorrect}': {suggestions}")
except enchant.errors.DictNotFoundError:
print("Error: Dictionary for the specified language was not found.")
print("Please ensure the Enchant C library and relevant language dictionaries are installed.")
except enchant.errors.EnchantError as e:
print(f"An Enchant error occurred: {e}")
print("Ensure the Enchant C library is properly installed and accessible.")