Python Glossary Русский

Python 3.10+Exceptionsexception

EncodingWarning

built-in exceptions

Opening a text file without an explicit encoding (relying on the locale).

Syntax

raise EncodingWarning

Added in Python 3.10 (PEP 597) and silent by default: you only see it when running with -X warn_default_encoding or with PYTHONWARNDEFAULTENCODING=1 set. The real issue it flags is portability — before Python 3.15, open(path) without encoding falls back to the locale encoding (UTF-8 since 3.15, PEP 686), so a file read as UTF-8 on Linux may be decoded as cp1251 on Windows and blow up with UnicodeDecodeError. The fix is never to catch it, but to pass encoding='utf-8' explicitly wherever you open text.

Examples

try:
    raise EncodingWarning
except EncodingWarning as e:
    print(type(e).__name__)   # → EncodingWarning

See also

Open in the full glossarydocs.python.orgReport an inaccuracy