{"record":{"id":"eb18f1cc6253498c","repo":"python/cpython","slug":"error-setting-lc-ctype-skipping-c-locale-coercion","errorCode":null,"errorMessage":"Error setting LC_CTYPE, skipping C locale coercion\\n","messagePattern":"Error setting LC_CTYPE, skipping C locale coercion\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"Python/pylifecycle.c","lineNumber":288,"sourceCode":"}\n\n\n#ifdef PY_COERCE_C_LOCALE\nstatic const char C_LOCALE_COERCION_WARNING[] =\n    \"Python detected LC_CTYPE=C: LC_CTYPE coerced to %.20s (set another locale \"\n    \"or PYTHONCOERCECLOCALE=0 to disable this locale coercion behavior).\\n\";\n\nstatic int\n_coerce_default_locale_settings(int warn, const _LocaleCoercionTarget *target)\n{\n    const char *newloc = target->locale_name;\n\n    /* Reset locale back to currently configured defaults */\n    _Py_SetLocaleFromEnv(LC_ALL);\n\n    /* Set the relevant locale environment variable */\n    if (setenv(\"LC_CTYPE\", newloc, 1)) {\n        fprintf(stderr,\n                \"Error setting LC_CTYPE, skipping C locale coercion\\n\");\n        return 0;\n    }\n    if (warn) {\n        fprintf(stderr, C_LOCALE_COERCION_WARNING, newloc);\n    }\n\n    /* Reconfigure with the overridden environment variables */\n    _Py_SetLocaleFromEnv(LC_ALL);\n    return 1;\n}\n#endif\n\nint\n_Py_CoerceLegacyLocale(int warn)\n{\n    int coerced = 0;\n#ifdef PY_COERCE_C_LOCALE","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Python/pylifecycle.c#L270-L306","documentation":"Emitted during C locale coercion in CPython startup (Python/pylifecycle.c, _coerce_default_locale_settings) when setenv(\"LC_CTYPE\", target_locale, 1) fails. The interpreter was trying to upgrade a legacy C locale to a UTF-8 capable one and could not export the LC_CTYPE variable, so it skips coercion entirely (returns 0) and continues with the original locale. This is a best-effort path: startup proceeds, only the coercion is abandoned.","triggerScenarios":"setenv failing: the process environment is immutable or malformed (environ replaced, insufficient memory for a new entry, restricted sandboxes that block putenv/setenv). Occurs only when PY_COERCE_C_LOCALE is compiled in, the runtime locale is C/POSIX, and a coercion target (e.g. C.UTF-8) exists.","commonSituations":"Minimal containers with tiny environments; embedded interpreters where the host application replaced `environ`; seccomp/sandbox profiles blocking setenv; exotic platforms with non-POSIX environment handling.","solutions":["Set a proper locale explicitly so coercion is never attempted: LANG=C.UTF-8 (or LC_CTYPE) in the container/service environment","If coercion is unwanted, disable it with PYTHONCOERCECLOCALE=0","In embedded scenarios, ensure environ is valid before Py_Initialize and that setenv is permitted"],"exampleFix":"# before (docker)\nENV LC_ALL=\n# after (docker)\nENV LANG=C.UTF-8","handlingStrategy":"fallback","validationCode":"# container: set a real locale so coercion is never attempted\nENV LANG=C.UTF-8","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set LANG/LC_CTYPE explicitly in containers, cron jobs, and service units","Use PYTHONCOERCECLOCALE=0 when you must keep the C locale","In embedded interpreters, keep environ intact and setenv-able before Py_Initialize"],"tags":["locale","environment","cpython","startup","utf-8"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}