A program must not depend on the console character set it happens to run under. Set the output encoding in code - one line at the top - so the whole class of encoding crashes becomes impossible rather than merely avoided.
By Pawan Nayar · from the LLOS AI Learning record
A program must not depend on the console character set it happens to run under.
Set the output encoding in code - one line at the top - so the whole class of encoding crashes becomes impossible rather than merely avoided.
Windows Python writes to a console that commonly uses cp1252. It has no arrow, no tick, no warning glyph, and no accented letter outside Latin-1. Printing one raises UnicodeEncodeError. macOS defaults to UTF-8, which hides the problem entirely until the code runs on Windows. The fix is one line at the top of anything that prints: import sys try: sys.stdout.reconfigure(encoding="utf-8") except Exception: pass Wrapped, because the guard must never be the thing that fails. **Why the older remedy - "use only ASCII in every runtime string" - is not enough.** It has to be remembered at every print, in every script, forever, by every person and every model. It fails the moment real data arrives rather than a literal you wrote: a shape name with an accent, a career title with a dash, a word from a dictionary of 200,000. And the audit says it does not hold in practice - 16 of 53 scripts print a character this console cannot render, so they crash the moment they run, and 31 of 53 print with no protection at all. The deciding evidence: this rule was broken three times in a single day, including inside the script written to detect the problem. A remedy that relies on memory loses to one line of code that makes the failure impossible. **Where it still matters what characters you use:** anything that is not stdout. A file gets its encoding at open time - always pass encoding="utf-8" explicitly rather than inheriting the platform default. Data written to disk, sent over an API, or stored in a database is a separate decision from what the console can display.
A build wrote sixteen pages, then printed a one-line summary containing a warning glyph. The console could not render it, the print raised UnicodeEncodeError, and the run ended with a traceback and a non-zero exit - reporting that all sixteen builds had failed. All sixteen were on disk and correct, and a full rebuild was very nearly started for nothing. One line at the top of the file would have made the crash impossible. Scanning every string for non-ASCII would have had to succeed sixteen times, forever, in every script.
More from the same record, pulled by shared topic — not written new for this page.
A developer must look for direct text changes. A developer must also look for code that creates file names or paths from parts.
An AI model should check each part of its output. For example, if there is a list of key phrases, the AI model must confirm each phrase is in the main text.
An AI model must always read a specific set of files. These files include 'mistakes_and_lessons.md'.
A developer must read specific API policy files before writing any code that makes external API calls. This prevents common mistakes.
A program must not depend on the console character set it happens to run under. Set the output encoding in code - one line at the top - so the whole class of encoding crashes becomes impossible rather than merely avoided.