all Python 3localefunction
locale.setlocale()
locale setup
Sets or queries the locale for a category (LC_NUMERIC, LC_ALL and others), which drives number separators, currency and collation.
Syntax
locale.setlocale(category, locale=None)
The main trap: the locale is state of the whole process, not of a module or thread. A call in one library silently changes atof(), format_string() and sorting across the program and in other threads, and setlocale itself is not thread-safe. Without the second argument it only reads the current value; setlocale(LC_ALL, '') adopts the locale from the environment, and a name the system lacks raises locale.Error. Until the first call every category except LC_CTYPE stays 'C'.
Examples
import locale
print(locale.setlocale(locale.LC_NUMERIC)) # → C — запрос, ничего не меняет
try:
locale.setlocale(locale.LC_NUMERIC, 'ru_RU.UTF-8')
print(locale.str(3.14)) # → ? 3,14 — если локаль есть в системе
except locale.Error as e:
print('нет такой локали:', e) # → ? unsupported locale setting
import locale
saved = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, '') # → ? локаль из окружения: LANG, LC_ALL
print(locale.localeconv()['decimal_point']) # → ? '.' или ',' — зависит от машины
locale.setlocale(locale.LC_ALL, saved) # вернуть как было — для всего процесса
print(locale.localeconv()['decimal_point']) # → .
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy