Python Locale Error: Unsupported Locale Setting

pip install - locale.Error: unsupported locale setting

The root cause is: your environment variable LC_ALL is missing or invalid somehow

Short answer-

just run the following command:

$ export LC_ALL=C

If you keep getting the error in new terminal windows, add it at the bottom of your .bashrc file.

Long answer-

Here is my locale settings:

$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=C

Python2.7

    $ uname -a
Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u6 (2015-11-09) x86_64 GNU/Linux
$ python --version
Python 2.7.9
$ pip --version
pip 8.1.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)
$ unset LC_ALL
$ pip install virtualenv
Traceback (most recent call last):
File "/usr/local/bin/pip", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/pip/__init__.py", line 215, in main
locale.setlocale(locale.LC_ALL, '')
File "/usr/lib/python2.7/locale.py", line 579, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
$ export LC_ALL=C
$ pip install virtualenv
Requirement already satisfied (use --upgrade to upgrade): virtualenv in /usr/local/lib/python2.7/dist-packages

python based Dockerfile throws locale.Error: unsupported locale setting

What I would do for Debian based docker image:

FROM python:3.7.5

RUN apt-get update && \
apt-get install -y locales && \
sed -i -e 's/# ru_RU.UTF-8 UTF-8/ru_RU.UTF-8 UTF-8/' /etc/locale.gen && \
dpkg-reconfigure --frontend=noninteractive locales

ENV LANG ru_RU.UTF-8
ENV LC_ALL ru_RU.UTF-8

and then in python:

import locale

locale.setlocale(locale.LC_ALL,'ru_RU.UTF-8')

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

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.)

unsupported locale setting Mac python

I think you can try to import the locale library first:

import locale

locale.setlocale(category=locale.LC_ALL,
locale="German" # Note: do not use "de_DE" as it doesn't work)

In your case, you would replace German by Spanish

You also might have some luck with:

try:
import locale
locale.setlocale(locale.LC_ALL, 'en_US.utf8')
except Exception:
try:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
except Exception as e:
messages.error(request, 'An error occurred: {0}'.format(e))

It is also possible to set LC_ALL in your environment by running the following command:

$ export LC_ALL=C

If that doesn't help, then you can try to install dpkg using sudo port install dpkg or brew (http://macappstore.org/dpkg/)

And then the following:

export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales

For your case, I believe the locale would be 'es_ES.UTF-8'

What is the correct way to set Python's locale on Windows?

It seems you're using Windows. The locale strings are different there. Take a more precise look at the doc:

locale.setlocale(locale.LC_ALL, 'de_DE') # use German locale; name might vary with platform

On Windows, I think it would be something like:

locale.setlocale(locale.LC_ALL, 'deu_deu')

MSDN has a list of language strings and of country/region strings



Related Topics



Leave a reply



Submit