ChatGPT解决这个技术问题 Extra ChatGPT

Python locale error: unsupported locale setting

Why do I get the following error when doing this in python:

>>> import locale
>>> print str( locale.getlocale() )
(None, None)
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/locale.py", line 531, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

This works with other locales like fr or nl as well. I'm using Ubuntu 11.04.

Update: Doing the following did not yield anything:

dpkg-reconfigure locales
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_CTYPE = "UTF-8",
    LANG = (unset)
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
Do you have a de_DE listed in locale -a? If not you must install it.
Related: a bash script to install all locales on Ubuntu.
locale.setlocale(locale.LC_ALL, 'de_DE') is wrong. You need locale.setlocale(locale.LC_ALL, 'de_DE.utf8').
You can sometimes discover an available encoding for the language/country you want using the built-in aliases: locale.setlocale(locale.LC_ALL, locale.locale_aliases['de_DE']).
I found the most appropriate answer here --> linuxpip.org/fix-python-locale-error

M
Muhammad Hassan

Run following commands

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

It will solve this.

Make sure to match the .UTF-8 part to the actual syntax found in the output of locale -a e.g. .utf8 on some systems.


I didn't have to use the dpkg command. After all, if the problem is occurring locally, then real solution would be to add the first two commands to your startup applications.
This didn't work for me. export LC_ALL="en_US.UTF-8" failed with -bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8) What worked was doing this: export LC_ALL=C
it's works for me also Thanks Muhammad Hassan
F
Fatemeh Abdollahei

According to this link, it solved by entering this command:

export LC_ALL=C


What does it mean? - ok it sets an environment varibale LC_ALL to the valuue 'C' - but why schould this work?
I search and try this every time when I change the computer and this works every time while other methods don't.
C
Community

You probably do not have any de_DE locale available.

You can view a list of available locales with the locale -a command. For example, on my machine:

$ locale -a
C
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
it_CH.utf8
it_IT.utf8
POSIX

Note that if you want to set the locale to it_IT you must also specify the .utf8:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'it_IT')   # error!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/locale.py", line 539, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting
>>> locale.setlocale(locale.LC_ALL, 'it_IT.utf8')
'it_IT.utf8'

To install a new locale use:

sudo apt-get install language-pack-id

where id is the language code (taken from here)

After you have installed the locale you should follow Julien Palard advice and reconfigure the locales with:

sudo dpkg-reconfigure locales

That did not work in my Debian docker-container. But this worked. stackoverflow.com/questions/28405902/…
A
Ayush Vatsyayan

One of the above answer provides the solution:

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

The problem with above solution is that it has to be done on the linux shell. However, if you are providing your code to work on the client machine then this is a bad approach. I also tried executing the above commands using os.system(), but still it doesn't work.

Solution that worked for me is

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

C
Community

More permanent solution would be to fill the missing values, in the output shown by command: locale

Output from locale is:

 $ locale
LANG=en_US.utf8
LANGUAGE=
LC_CTYPE="en_US.utf8"
LC_NUMERIC=es_ES.utf8
LC_TIME=es_ES.utf8
LC_COLLATE="en_US.utf8"
LC_MONETARY=es_ES.utf8
LC_MESSAGES="en_US.utf8"
LC_PAPER=es_ES.utf8
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT=es_ES.utf8
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=

To Fill the missing values edit ~/.bashrc :

 $ vim ~/.bashrc

Add the following lines after the above command (suppose you want en_US.UTF-8 to be your language):

export LANGUAGE="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"

If this file is ReadOnly you would be needing to follow the steps mentioned by The GeekyBoy. The answer given by Dr Beco in Superuser has details relating to saving readonly files.

After saving the file do:

$ source ~/.bashrc

Now you wont be facing the same problem anymore.


J
Julien Palard

If you're on a Debian (or Debian fork), you can add locales using :

dpkg-reconfigure locales

Does the dpkg-reconfigure locales bring you a local choosing menu like this one : vpser.net/uploads/2013/01/dpkg-reconfigure-locales-1.jpg ?
Nope. I just get the output I posted in the update of my question.
M
Mohideen bin Mohammed

You error clearly says, you are trying to use locale something was not there.

>>> locale.setlocale(locale.LC_ALL, 'de_DE')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/locale.py", line 581, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

locale.Error: unsupported locale setting

To check available setting, use locale -a

deb@deb-Latitude-E7470:/ambot$ locale -a
C
C.UTF-8
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
POSIX

so you can use one among,

>>> locale.setlocale(locale.LC_ALL, 'en_AG.utf8')
'en_AG.utf8'
>>> 

for de_DE

This file can either be adjusted manually or updated using the tool, update-locale.

update-locale LANG=de_DE.UTF-8

I want to add persian - farsi to my locale. I run sudo update-locale LANG=fa_IR and sudo update-locale LANG=fa_IR.UTF-8 in terminal. nothing happens. not an error nor a completion notification. what should I do?
K
Keith Smiley

On Arch Linux I was able to fix this by running sudo locale-gen


k
kikeenrique

For the record, I had this same problem, but none of the solutions worked. I had upgraded my computer and migrated my PC. I had a a mixed locale english and spanish:

$ locale
LANG=en_US.utf8
LANGUAGE=
LC_CTYPE="en_US.utf8"
LC_NUMERIC=es_ES.utf8
LC_TIME=es_ES.utf8
LC_COLLATE="en_US.utf8"
LC_MONETARY=es_ES.utf8
LC_MESSAGES="en_US.utf8"
LC_PAPER=es_ES.utf8
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT=es_ES.utf8
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=

But, on my new Debian installation, I just selected english as locale. Which finally worked was to reconfigure locales package to add and generate spanish too.

$ grep -v "#" /etc/locale.gen 
en_US.UTF-8 UTF-8
es_ES.UTF-8 UTF-8

u
user3780002

In my opinion, the easiest way to setup the local locale in python{,3} is:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'de_DE.UTF-8'

Then, locale aware stuff just works, if you're on a decent linux distro, and should work on binary distributions of the other OSes as well (or that's a bug IMHO).

>>> import datetime as dt
>>> print(dt.date.today().strftime("%A %d. %B %Y"))
Sonntag 11. Dezember 2016

S
Seenu S

Place it in the Dockerfile above the ENV.

# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default
RUN apt-get update && apt-get install -y locales && rm -rf /var/lib/apt/lists/* \
    && localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8

ENV LANG en_US.UTF-8

Thanks, I tried the other solutions (dpkg-reconfigure locales, locale-gen) and nothing else worked in my debian:9 container. Is this really a / the standard way to create the en_US.UTF-8 locale?
Yes, I think its the standard way en_US.UTF-8 locale. When you run locale in terminal u get `LANG=en_US.UTF-8 and other locale? stackoverflow.com/questions/36394101/…
S
Saroj Rai

Just open the .bashrc file and add this

export LC_ALL=C

and then type source .bashrc in terminal.


a
andy

run this command locale to get what locale is used. Such as:

LANG=en_US.UTF-8 LANGUAGE=en_US:en LC_CTYPE=zh_CN.UTF-8 LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_NAME="en_US.UTF-8" LC_ADDRESS="en_US.UTF-8" LC_TELEPHONE="en_US.UTF-8" LC_MEASUREMENT="en_US.UTF-8" LC_IDENTIFICATION="en_US.UTF-8" LC_ALL=

search for the listed locales list in first step in /etc/locale-gen file. Uncomment to used ones

run locale-gen to generate newly added locales


i
imrek

This error can occur, if you have just added a new locale. You need to restart the python interactive shell (quit() and python) to get access to it.


C
Carlos

If I were you, I would use BABEL: http://babel.pocoo.org/en/latest/index.html

I got the same issue here using Docker, I've tried every single step and didn't work well, always getting locale error, so I decided to use BABEL, and everything worked well.


P
Peter F

python looks for .UFT-8, but you probably have .utf8 try installing the .UFT-8 packages with sudo dpkg-reconfigure locales


S
Sahar

For those deploying a docker image and using a locale that isn't shown in the locale -a command, add this line to your Dockerfile
RUN apt-get install -y locales

This should add all locales to your image, I used de_DE which is not part of AWS default Ubuntu server.


C
Community

In trying to get python to spit out names in specific locale I landed here with same problem.

In pursuing the answer, things got a little mystical I find.

I found that python code.

import locale
print locale.getdefaultlocale()
>> ('en_DK', 'UTF-8')

And indeed locale.setlocale(locale.LC_TIME, 'en_DK.UTF-8') works

Using tips here I tested further to see what is available using python code

import locale
loc_list = [(a,b) for a,b in locale.locale_alias.items() ]
loc_size = len(loc_list)
print loc_size,'entries'

for loc in loc_list:
    try:
        locale.setlocale(locale.LC_TIME, loc[1])
        print 'SUCCES set {:12} ({})'.format(loc[1],loc[0])
    except:
        pass

which yields

858 entries
SUCCES set en_US.UTF-8  (univ)
SUCCES set C            (c.ascii)
SUCCES set C            (c.en)
SUCCES set C            (posix-utf2)
SUCCES set C            (c)
SUCCES set C            (c_c)
SUCCES set C            (c_c.c)
SUCCES set en_IE.UTF-8  (en_ie.utf8@euro)
SUCCES set en_US.UTF-8  (universal.utf8@ucs4)
SUCCES set C            (posix)
SUCCES set C            (english_united-states.437)
SUCCES set en_US.UTF-8  (universal)

Of which only above is working! But the en_DK.UTF-8 is not in this list, though it works!?!? What?? And the python generated locale list do contain a lot of combos of da and DK, which I am looking for, but again no UTF-8 for da/DK...

I am on a Point Linux distro (Debian based), and here locale says amongst other LC_TIME="en_DK.UTF-8", which I know works, but not the locale I need.

locale -a says

C
C.UTF-8
en_DK.utf8
en_US.utf8
POSIX

So definitely need to install other locale, which i did by editing /etc/locale.gen, uncomment needed line da_DK.UTF-8 UTF-8 and run command locale-gen

Now locale.setlocale(locale.LC_TIME, 'da_DK.UTF-8') works too, and I can get my localized day and month names.

My Conclision:

Python : locale.locale_alias is not at all helpfull in finding available locales!!!

Linux : It is quite easy to get locale list and install new locale. A lot of help available.

Windows : I have been investigating a little, but nothing conclusive. There are though posts leading to answers, but I have not felt the urge to pursue it.


T
Torge Husfeldt

if I understand correctly, the main source of error here is the exact syntax of the locale-name. Especially as it seems to differ between distributions. I've seen mentioned here in different answers/comments:

de_DE.utf8
de_DE.UTF-8

Even though this is obviously the same for a human being, the same does not hold for your standard deterministic algorithm.

So you will probably do something along the lines of:

DESIRED_LOCALE=de
DESIRED_LOCALE_COUNTRY=DE
DESIRED_CODEPAGE_RE=\.[Uu][Tt][Ff].?8
if [ $(locale -a | grep -cE "${DESIRED_LOCALE}_${DESIRED_LOCALE_COUNTRY}${DESIRED_CODEPAGE_RE}") -eq 1 ]
then
    export LC_ALL=$(locale -a | grep -m1 -E "${DESIRED_LOCALE}_${DESIRED_LOCALE_COUNTRY}${DESIRED_CODEPAGE_RE}")
    export LANG=$LC_ALL
else
    echo "Not exactly one desired locale definition found: $(locale -a | grep -E "${DESIRED_LOCALE}_${DESIRED_LOCALE_COUNTRY}${DESIRED_CODEPAGE_RE}")" >&2
fi

I
Inder

first, make sure you have the language pack installed by doing :

sudo apt-get install language-pack-en-base


sudo dpkg-reconfigure locales

the english languagepack is installed per default - furthermore the answer is allready given here and this one lacks in formating.
t
thiagola92

Not the answer for this question but this question helped me to find the answer for my problem.

I had this problem when using inside a Docker container. I solved by installing locales, adding my language to the locale.gen file, executing locale-gen (it reads from locale.gen) and finally setting LANG to my language.

For example, my Dockerfile:

RUN apt-get install -y locales
RUN echo "pt_BR.UTF-8 UTF-8" >> /etc/locale.gen
RUN locale-gen pt_BR.UTF-8
ENV LANG=pt_BR.UTF-8

This didn't work for me, but RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && export LC_ALL=C && locale-gen did.