Python 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
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’]) .
21 Answers 21
export LC_ALL="en_US.UTF-8" export LC_CTYPE="en_US.UTF-8" sudo dpkg-reconfigure locales
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
According to this link, it solved by entering this command:
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.
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 "", line 1, in 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/…
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')
More permanent solution would be to fill the missing values, in the output shown by command: locale
$ 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 :
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.
Now you wont be facing the same problem anymore.
If you’re on a Debian (or Debian fork), you can add locales using :
Does the dpkg-reconfigure locales bring you a local choosing menu like this one : vpser.net/uploads/2013/01/dpkg-reconfigure-locales-1.jpg ?
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 "", line 1, in 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
>>> locale.setlocale(locale.LC_ALL, 'en_AG.utf8') 'en_AG.utf8' >>>
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?
On Arch Linux I was able to fix this by running sudo locale-gen
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
In my opinion, the easiest way to setup the local locale in python 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
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/…
Just open the .bashrc file and add this
and then type source .bashrc in terminal.
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
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.
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.
python looks for .UFT-8, but you probably have .utf8 try installing the .UFT-8 packages with sudo dpkg-reconfigure locales
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.
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.
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 (<>)'.format(loc[1],loc[0]) except: pass
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.
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.