Skip to content
Advertisement

Python setlocale with empty string (default locale) gives “unsupported locale setting”

I’m getting the following error:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
Traceback (most recent call last):
    ...
locale.Error: unsupported locale setting

The setup is a newly installed and updated Fedora 34 system with Python 3.9.6. The error occurs when running Lutris, and also when typing the commands into IDLE. Here is the exact backtrace from Lutris:

lutris:33:<module>:BrokenPipeError: [Errno 32] Broken pipe

Traceback (most recent call last):
  File "/usr/bin/lutris", line 31, in <module>
    locale.setlocale(locale.LC_ALL, "")
  File "/usr/lib64/python3.9/locale.py", line 610, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/bin/lutris", line 33, in <module>
    sys.stderr.write("Unsupported locale setting. Fix your localesn")
BrokenPipeError: [Errno 32] Broken pipe

Given that, according to the Python docs, “An empty string specifies the user’s default settings,” I figured maybe my default locale (en_AU.UTF-8) wasn’t supported at some level of the stack. But the following all work fine:

>>> locale.getdefaultlocale()
('en_AU', 'UTF-8')
>>> locale.setlocale(locale.LC_ALL, locale.getdefaultlocale())
'en_AU.UTF-8'
>>> locale.setlocale(locale.LC_ALL, 'en_AU.UTF-8')
'en_AU.UTF-8'

And oddly, the error doesn’t happen when I run python in a terminal and type the commands there!

>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'LC_CTYPE=en_AU.UTF-8;LC_NUMERIC=en_AU.UTF-8;LC_TIME=C.UTF-8;LC_COLLATE=en_AU.UTF-8;LC_MONETARY=en_AU.UTF-8;LC_MESSAGES=en_AU.UTF-8;LC_PAPER=en_AU.UTF-8;LC_NAME=en_AU.UTF-8;LC_ADDRESS=en_AU.UTF-8;LC_TELEPHONE=en_AU.UTF-8;LC_MEASUREMENT=en_AU.UTF-8;LC_IDENTIFICATION=en_AU.UTF-8'

So I have no idea what locale Python thinks it’s setting when I run IDLE or Lutris from the desktop, but it’s evidently not the same one set when I use the terminal. How can I debug this error?

(Incidentally, running Lutris from the command line has been a successful workaround.)

Advertisement

Answer

The problem is the environment variable LC_TIME. Apparently I have it set to en_150.UTF-8 from the desktop, but to C.UTF-8 from the terminal.

Cimbali’s comment set me on the right track. I looked at the environment variables using os.environ, within both IDLE-started-from-the-desktop and Python-started-from-the-terminal.

The ones that appeared locale-related were LANG, LANGUAGE, and LC_TIME. The first two were the same between the two cases, but the last one differed.

And this made the error go away:

>>> os.environ['LC_TIME'] = 'C.UTF-8'
>>> locale.setlocale(locale.LC_ALL, '')

Which just leaves me wondering, what the heck is en_150? Is it because I have my desktop set to 24-hour time rather than the default? (Also wondering whether a bug report/request for more descriptive error messages would be useful for Python.)

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement