all Python 3localefunction
locale.getdefaultlocale()
locale setup
Guesses the user's locale from the LC_ALL, LC_CTYPE, LANG and LANGUAGE environment variables and returns (language code, encoding).
Syntax
locale.getdefaultlocale(envvars=('LC_ALL', 'LC_CTYPE', 'LANG', 'LANGUAGE'))
It reads environment variables, not the process's active locale, so it disagrees with getlocale() after setlocale() and knows nothing of UTF-8 mode. It was deprecated in 3.11 with a plan to drop it in 3.15, and on 3.11-3.13 a call prints DeprecationWarning; 3.15 (and recent 3.14 releases) reverted the deprecation. For the encoding prefer getencoding(), for the locale setlocale(LC_ALL, '') plus getlocale().
Examples
import locale
import warnings
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning) # 3.11–3.13 предупреждают
print(locale.getdefaultlocale()) # → ? ('ru_RU', 'UTF-8') — по переменным окружения
print(locale.getlocale()) # → ? — а это действующая локаль процесса
See also
Open in the full glossarydocs.python.orgReport an inaccuracy