Python 3.5配置pylint与pre-commit遇Unicode解码错误求助
Alright, let's break down what's going on here and how to fix that frustrating UnicodeDecodeError. This isn't likely a straight-up bug in pylint or pre-commit—instead, it's tied to encoding quirks in older software versions and environment settings.
What's Triggering the Error?
That 'ascii' codec can't decode byte 0xc2 message tells us pylint is trying to read content with non-ASCII characters (the 0xc2 byte is part of UTF-8 encoded symbols like accented letters or special characters) but is stuck using ASCII as the default encoding. Here's why that might be happening:
- Outdated pylint version: pylint 2.4.4 is from 2019, and it has known gaps in handling UTF-8 content compared to newer releases. Combined with Python 3.5.2 (which is end-of-life and also has encoding limitations), this creates a perfect storm for decoding issues.
- Environment defaulting to ASCII: Python 3.5 might fall back to ASCII encoding in some older OS setups, even when your files are saved in UTF-8. When pylint tries to read those files, it chokes on non-ASCII bytes.
- Problematic
$PROJECT_ROOTvariable: pre-commit doesn't natively expand shell variables like$PROJECT_ROOTin the config file. This might be causing pylint to look for a malformed.pylintrcpath, which could indirectly trigger encoding errors (though this is less likely than the first two points).
Step-by-Step Fixes
1. Upgrade to a newer (still Python 3.5-compatible) pylint version
Pylint 2.13.4 is the last release that supports Python 3.5, and it has much better encoding handling than 2.4.4. Update your .pre-commit-config.yaml to use this version:
repo: https://github.com/pycqa/pylint rev: pylint-2.13.4 hooks: - id: pylint args: - --limit=8 - --rcfile=.pylintrc # Drop $PROJECT_ROOT—pre-commit runs from your repo root by default
Then run pre-commit autoupdate to pull the new version, and try pre-commit run again.
2. Force UTF-8 encoding in your environment
If upgrading isn't an option right now, you can explicitly set your shell environment to use UTF-8 before running pre-commit:
export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8 pre-commit run
To make this permanent, add those two export lines to your shell profile (like ~/.bashrc or ~/.zshrc) so they apply every time you open a terminal.
3. Double-check your files' encoding
The 0xc2 byte points to non-ASCII content in one of your project files. Make sure all your code files are saved with UTF-8 encoding (most modern editors default to this, but older setups might not). If you find any special characters causing issues, you can either replace them or ensure the file's encoding is set correctly.
4. Fix the .pylintrc path
As mentioned, pre-commit doesn't expand $PROJECT_ROOT in the config. If your .pylintrc is in your repo root, just use .pylintrc as the path. If it's in a subdirectory (like config/.pylintrc), use the relative path from the repo root.
Why These Fixes Work
Newer pylint versions enforce UTF-8 by default when reading files, eliminating the reliance on system default encodings that might be set to ASCII. Setting the LC_ALL and LANG variables forces Python (and pylint) to use UTF-8 regardless of the system's default, which bypasses the decode error entirely.
内容的提问来源于stack exchange,提问作者saadi

