Skip to content
Latchkey

Python "UnicodeEncodeError: ascii codec" (CI locale) in CI

The runner has no UTF-8 locale configured, so Python picks ASCII for stdout or file encoding. The moment your program emits a non-ASCII character, the ASCII codec cannot encode it and raises.

What this error means

Code that prints or writes Unicode fails with "UnicodeEncodeError: 'ascii' codec can't encode character" only in CI, while it works on a developer machine with a UTF-8 locale.

python
UnicodeEncodeError: 'ascii' codec can't encode character '\u2713' in position 4:
ordinal not in range(128)

Common causes

The runner locale is C / POSIX (ASCII)

A minimal image sets LANG/LC_ALL to C, so Python's preferred encoding is ASCII and any non-ASCII output fails.

Writing to a file without an explicit encoding

open(path, "w") uses the locale encoding; on an ASCII-locale runner it cannot write Unicode content.

How to fix it

Set a UTF-8 locale for the job

Export UTF-8 locale variables (or enable Python UTF-8 mode) so stdout and file I/O default to UTF-8.

.github/workflows/ci.yml
env:
  LANG: C.UTF-8
  LC_ALL: C.UTF-8
  PYTHONUTF8: '1'

Specify encoding explicitly when writing

Pass encoding="utf-8" so the call does not depend on the runner locale.

app/io.py
with open(path, "w", encoding="utf-8") as f:
    f.write(text)

How to prevent it

  • Set PYTHONUTF8=1 or a UTF-8 locale in the CI environment.
  • Always pass encoding="utf-8" to open().
  • Do not rely on the developer-machine locale leaking into CI.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →