Codespell is a Python command-line tool designed to find and fix common misspellings in text files, primarily within source code. It operates using a curated dictionary of common typos and their corrections, aiming to reduce false positives that typical spell checkers might produce with technical terms or variable names. The library is actively maintained and frequently integrated into CI/CD pipelines and pre-commit hooks for automated code quality checks.
pip install codespellNo compatibility data collected yet for this library.
To get started, run `codespell` from your terminal in the directory you wish to check. By default, it performs a dry run, listing potential misspellings. Use the `-i` flag for interactive mode to review and confirm changes, or `-w` to automatically apply corrections (use with caution).
Always run `codespell` with `-i` (e.g., `codespell -i 3 .`) when planning to apply fixes, or review the dry run output thoroughly before using `-w`.
Ensure that `~/.local/bin` is included in your system's `$PATH` environment variable. You might need to add `export PATH="$HOME/.local/bin:$PATH"` to your shell's configuration file (e.g., `.bashrc`, `.zshrc`).
If you are using Python < 3.11 and configure `codespell` via `pyproject.toml`, ensure `pip install tomli` is executed. Python 3.11 and later include `tomli` in the standard library.
Always include a trailing comma after the last suggestion when defining dictionary entries with multiple corrections. Refer to the dictionary format documentation for details.
Add the Python user scripts directory (e.g., `~/.local/bin` on Linux/macOS or `%APPDATA%\Python\Scripts` on Windows) to your system's PATH, or ensure `codespell` is installed in the active virtual environment by running `pip install codespell`.
Ensure `codespell` is installed within the environment where it's being called (e.g., the virtual environment for a pre-commit hook) and that the system's PATH includes the directory where `codespell` is installed. For pre-commit, verify the `rev` in `.pre-commit-config.yaml` is correct and allow `pre-commit` to manage its environment.
Explicitly specify the correct file encoding using the `--encoding` flag (e.g., `codespell --encoding=utf-8 your_file.py`) or configure `codespell` to detect encoding automatically if supported by your version.
Ignore specific words using the `-L` flag (e.g., `codespell -L yourword,anotherword`), by providing a file with words to ignore via `-I` (e.g., `codespell -I .codespellignore`), or by configuring `ignore-words-list` in a `.codespellrc` or `pyproject.toml` configuration file for persistent project-wide ignores.