[This answer is target on linux platform only]
The first thing you should know is most of the locale config file located path can be get from localedef --help
:
$ localedef --help | tail -n 5
System's directory for character maps : /usr/share/i18n/charmaps
repertoire maps: /usr/share/i18n/repertoiremaps
locale path : /usr/lib/locale:/usr/share/i18n
For bug reporting instructions, please see:
<https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs>
See the last /usr/share/i18n
? This is where your xx_XX.UTF-8 config file located:
$ ls /usr/share/i18n/locales/zh_*
/usr/share/i18n/locales/zh_CN /usr/share/i18n/locales/zh_HK /usr/share/i18n/locales/zh_SG /usr/share/i18n/locales/zh_TW
Now what ? We need to compile them into archive binary. One of the way, e.g. assume I have /usr/share/i18n/locales/en_LOVE
, I can add it into compile list, i.e. /etc/locale-gen
file:
$ tail -1 /etc/locale.gen
en_LOVE.UTF-8 UTF-8
And compile it to binary with sudo locale-gen
:
$ sudo locale-gen
Generating locales (this might take a while)...
en_AG.UTF-8... done
en_AU.UTF-8... done
en_BW.UTF-8... done
...
en_LOVE.UTF-8... done
Generation complete.
And now update the system default locale with desired LANG
, LC_ALL
…etc with this update-locale
:
sudo update-locale LANG=en_LOVE.UTF-8
update-locale
actually also means to update this /etc/default/locale
file which will source by system on login to setup environment variables:
$ head /etc/default/locale
# File generated by update-locale
LANG=en_LOVE.UTF-8
LC_NUMERIC="en_US.UTF-8"
...
But we may not want to reboot to take effect, so we can just source it to environment variable in current shell session:
$ . /etc/default/locale
How about sudo dpkg-reconfigure locales
? If you play around it you will know this command basically act as GUI to simplify the above steps, i.e. Edit /etc/locale.gen
-> sudo locale-gen
-> sudo update-locale LANG=en_LOVE.UTF-8
For python, as long as /etc/locale.gen
contains that locale candidate and locale.gen
get compiled, setlocale(category, locale)
should work without throws locale.Error: unsupoorted locale setting
. You can check the correct string en_US.UTF-8
/en_US/....etc
to be set in setlocale()
, by observing /etc/locale.gen
file, and then uncomment and compile it as desired. zh_CN GB2312
without dot in that file means the correct string is zh_CN
and zh_CN.GB2312
.
When running a Python script, you may encounter the «unsupported locale setting» error, which typically appears as follows:
locale.Error: unsupported locale setting
This error occurs because the locale settings of your system are not supported by Python’s locale module. The locale module uses the locale settings to perform locale-aware operations, such as formatting dates and times, currency, and sorting strings in a specific language-sensitive order.
Method 1: Set the locale environment variables
If you are facing the «unsupported locale setting» error in Python, it means that your system does not have the required locale settings installed. One way to fix this issue is by setting the locale environment variables. Here’s how you can do it:
- Import the
locale
module:
- Set the locale environment variables:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
Here, we are setting the locale to en_US.UTF-8
. You can replace this with the locale that you want to use.
- Verify the locale:
print(locale.getlocale())
This will print the current locale settings. You should see the locale that you set in step 2.
- Use the
locale
module in your code:
formatted_number = locale.format_string("%d", 123456789, grouping=True)
print(formatted_number)
Here, we are using the locale.format_string
method to format a number with grouping (thousands separator). This will use the locale settings that we set in step 2.
That’s it! You have successfully set the locale environment variables in Python. Here’s the complete code:
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
print(locale.getlocale())
formatted_number = locale.format_string("%d", 123456789, grouping=True)
print(formatted_number)
Method 2: Install the missing locale
To fix the Python locale error «unsupported locale setting», you can try installing the missing locale. Here are the steps to do it:
-
Check the current locale settings by running the command
locale
in the terminal. You will see a list of locale settings, such as LANG, LC_ALL, LC_CTYPE, etc. Take note of the current settings. -
Identify the missing locale. The error message usually indicates which locale is missing. For example, it could be «en_US.UTF-8» or «de_DE.UTF-8».
-
Install the missing locale by running the following command in the terminal:
Replace
<locale>
with the missing locale, such as «en_US.UTF-8» or «de_DE.UTF-8». This command generates the locale files for the specified locale. -
Update the locale settings by running the following command in the terminal:
This command updates the system-wide locale settings based on the current values of environment variables.
-
Verify that the missing locale is now available by running the command
locale -a
in the terminal. You should see the missing locale in the list of available locales.
Here’s an example code snippet that installs the missing locale «en_US.UTF-8»:
$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.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=
$ sudo locale-gen en_US.UTF-8
Generating locales (this might take a while)...
en_US.UTF-8... done
Generation complete.
$ sudo update-locale
Generating locales (this might take a while)...
en_US.UTF-8... up-to-date
Generation complete.
$ locale -a
C
en_US.utf8
POSIX
In this example, the missing locale «en_US.UTF-8» was installed successfully and is now available in the list of available locales.
Method 3: Use a default locale
To fix the Python locale error «unsupported locale setting», you can use a default locale. Here are the steps to do it:
- Import the locale module:
- Set the default locale using the setlocale() function. This function takes two arguments: the first argument is the category, which specifies the type of locale to use (e.g. LC_ALL, LC_CTYPE, LC_NUMERIC, etc.), and the second argument is the locale to use (e.g. «en_US.UTF-8», «es_ES.UTF-8», etc.). In this case, we will set the default locale to «C»:
locale.setlocale(locale.LC_ALL, 'C')
- Now you can try running your Python program again, and the locale error should be resolved.
Here is the complete code:
import locale
locale.setlocale(locale.LC_ALL, 'C')
Note that setting the default locale to «C» may not be appropriate for all situations. You may need to set the locale to a different value depending on your specific needs.
Method 4: Change the default locale for Python
If you are getting a «unsupported locale setting» error in Python, you can fix it by changing the default locale for Python. Here are the steps to do it:
- Import the locale module:
- Set the default locale to a valid one. For example, «en_US.UTF-8» for English language and UTF-8 encoding:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
- Test if the default locale is set correctly by printing it:
print(locale.getlocale())
- If you get a «locale.Error: unsupported locale setting» error, it means that the locale you set is not available on your system. You can check the available locales by running the following command in your terminal:
- Choose a valid locale from the list and set it as the default locale:
locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')
- Test again if the default locale is set correctly:
print(locale.getlocale())
- If you want to change the default locale permanently, you can add the following line to your ~/.bashrc file:
export LC_ALL=en_US.UTF-8
- Save the file and run the following command to reload it:
- Now you should be able to run your Python code without getting the «unsupported locale setting» error.
Here’s the complete code:
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
print(locale.getlocale())
locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')
print(locale.getlocale())
I hope this helps you fix the «unsupported locale setting» error in Python. Let me know if you have any questions!
hi @LoveBootCaptain ,
i saw your project by twitter, and at the same i see this project here.i’m a beginner in python,too. So i try to build this project. And i encounter an error:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\WeatherPi_TFT-master\WeatherPi_TFT.py", line 16, in <module>
locale.setlocale(locale.LC_ALL, locale.getdefaultlocale())
File "D:\Python35\lib\locale.py", line 594, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
i dont know where is wrong.However,i annotate return _setlocale(category, locale)
in «D:\Python35\lib\locale.py», line 594
and the error is gone
But there is a new error:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\WeatherPi_TFT-master\WeatherPi_TFT.py", line 637, in <module>
loop()
File "C:\Users\Administrator\Desktop\WeatherPi_TFT-master\WeatherPi_TFT.py", line 602, in loop
Update.run()
File "C:\Users\Administrator\Desktop\WeatherPi_TFT-master\WeatherPi_TFT.py", line 410, in run
Update.update_json()
File "C:\Users\Administrator\Desktop\WeatherPi_TFT-master\WeatherPi_TFT.py", line 212, in update_json
config = json.loads(config_data)
File "D:\Python35\lib\json\__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "D:\Python35\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "D:\Python35\lib\json\decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 1 (char 59)
i have no idea about it…
PS: i can get defaultlocale by locale.getdefaultlocale()
:
>>> locale.getdefaultlocale()
('zh_CN', 'cp936')
Do you have a idea?
Answer by Calum Campbell
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.,To install a new locale use:,This file can either be adjusted manually or updated using the tool, update-locale.,run locale-gen to generate newly added locales
Run following commands
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales
Answer by Layton Dodson
While you can set the locale exporting an environment variable, you will have to do that every time you start a session, meaning after a restart, things would go back the same as before. Setting a locale the following way will solve the problem permanently.,You’ve been fiddling with the environment variables then try to use pip,The root cause is: your environment variable LC_ALL is missing or invalid somehow,If you want to solve it quick, you can sequentially run the commands below in the terminal :
The root cause is: your environment variable LC_ALL is missing or invalid somehow
.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}➜ ~ pip install virtualenv
Traceback (most recent call last):
File "/usr/bin/pip", line 11, in <module>
sys.exit(main())
File "/usr/lib/python3.4/site-packages/pip/__init__.py", line 215, in main
locale.setlocale(locale.LC_ALL, '')
File "/usr/lib64/python3.4/locale.py", line 592, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
Code language: Bash (bash)
If you want to solve it quick, you can sequentially run the commands below in the terminal :
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure localesCode language: Bash (bash)
Here is a one-liner for you the lazy guys :
export LC_ALL="en_US.UTF-8" & export LC_CTYPE="en_US.UTF-8" & sudo dpkg-reconfigure localesCode language: Bash (bash)
The output should look like this :
~$ export LC_ALL="en_US.UTF-8" & export LC_CTYPE="en_US.UTF-8" & sudo dpkg-reconfigure locales
[1] 7201
[2] 7202
[sudo] password for user:
Generating locales (this might take a while)...
en_AG.UTF-8... done
en_AU.UTF-8... done
en_BW.UTF-8... done
en_CA.UTF-8... done
en_DK.UTF-8... done
en_GB.UTF-8... done
en_HK.UTF-8... done
en_IE.UTF-8... done
en_IL.UTF-8... done
en_IN.UTF-8... done
en_NG.UTF-8... done
en_NZ.UTF-8... done
en_PH.UTF-8... done
en_SG.UTF-8... done
en_US.UTF-8... done
en_ZA.UTF-8... done
en_ZM.UTF-8... done
en_ZW.UTF-8... done
vi_VN.UTF-8... done
Generation complete.
[1]- Done export LC_ALL="en_US.UTF-8"
[2]+ Done export LC_CTYPE="en_US.UTF-8"
Code language: Bash (bash)
First, you need to install locales
package :
sudo apt-get install locales -yCode language: Bash (bash)
Then use the locale-gen command to generate locales :
sudo locale-gen en_US.UTF-8Code language: Bash (bash)
After that, permanently set the configuration to the system
sudo echo "LANG=en_US.UTF-8" > /etc/default/localeCode language: Bash (bash)
Just add these lines to your Dockerfile
Dockerfile#
Set the locale
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
Code language: Dockerfile (dockerfile)
After setting the locale, verify that the system is updated by using the following command :
~$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.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=en_US.UTF-8
Code language: Bash (bash)
Answer by Amaris Lozano
this means the environment variable LC_ALL is missing or invalid somehow.,LC_ALL is the environment variable that overrides the value of the LANG and the values of any other LC_* environment variables.,If you see the following error while installing pip:
,well explained.. it overrides all the other localisation settings.
Traceback (most recent call last):
File "/usr/bin/pip", line 11, in <module>
sys.exit(main())
File "/usr/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 581, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
Answer by Noor Correa
Ok, so I’ve just encountered the following error on my newly installed Ubuntu Machine: locale.Error: unsupported locale setting when I was trying create a virtual environment with python-virtualenv. After some googling i found the following solution:,Digital Designer, Backend and Frontend developer from Sweden with experience in usability.
Ok, so I’ve just encountered the following error on my newly installed Ubuntu Machine: locale.Error: unsupported locale setting when I was trying create a virtual environment with python-virtualenv. After some googling i found the following solution:
export LANGUAGE=en_US.UTF-8export LANG=en_US.UTF-8export LC_ALL=en_US.UTF-8locale-gen en_US.UTF-8sudo dpkg-reconfigure locales
Answer by Otis Bonilla
The reason is that the system lacks the corresponding language package, which needs to be downloaded and installed.,locale.Error: unsupported locale setting locale,Error: the solution set by unsupported locale
0. References 1. Cause of error 2. Solution,0. References
https://stackoverflow.com/questions/14547631/python-locale-error-unsupported-locale-setting
1. Report the cause of the error
The ubuntu 16.04
installed on the vagrant
+ virtualbox
installed on the ubuntu 16.04
used the pip3 list
and the python3-m venv venv
both commands gave the error message as follows:
[email protected]:~/microblog$ pip3 list
Traceback (most recent call last):
File "/usr/bin/pip3", line 11, in <module>
sys.exit(main())
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 215, in main
locale.setlocale(locale.LC_ALL, '')
File "/usr/lib/python3.5/locale.py", line 594, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
2. Solutions
Use the locale
locale to view the current language Settings:
[email protected]:~$ locale
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=zh_CN.UTF-8
LC_TIME=zh_CN.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=zh_CN.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_TELEPHONE=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
LC_IDENTIFICATION=zh_CN.UTF-8
LC_ALL=
It is found that there are two languages in this setting, one is en_us.utf-8
, and the other is zh_cn.utf-8
.
Use locale -a
to view all available languages in the current system:
[email protected]:~$ locale -a
C
C.UTF-8
en_US.utf8
id_ID.utf8
POSIX
It was found that zh_cn.utf-8
is missing in the available language above, and this is the reason for the error.
Use sudo apt install language-pack-zh-hans
installation language:
[email protected]:~$ sudo apt install language-pack-zh-hans
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
language-pack-zh-hans-base
The following NEW packages will be installed:
language-pack-zh-hans language-pack-zh-hans-base
0 upgraded, 2 newly installed, 0 to remove and 3 not upgraded.
Need to get 2110 kB of archives.
After this operation, 8545 kB of additional disk space will be used.
Do you want to continue?[Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 language-pack-zh-hans-base all 1:16.04+20160627 [2108 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 language-pack-zh-hans all 1:16.04+20160627 [1870 B]
Fetched 2110 kB in 3s (567 kB/s)
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_TIME = "zh_CN.UTF-8",
LC_MONETARY = "zh_CN.UTF-8",
LC_ADDRESS = "zh_CN.UTF-8",
LC_TELEPHONE = "zh_CN.UTF-8",
LC_NAME = "zh_CN.UTF-8",
LC_MEASUREMENT = "zh_CN.UTF-8",
LC_IDENTIFICATION = "zh_CN.UTF-8",
LC_NUMERIC = "zh_CN.UTF-8",
LC_PAPER = "zh_CN.UTF-8",
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
locale: Cannot set LC_ALL to default locale: No such file or directory
Selecting previously unselected package language-pack-zh-hans-base.
(Reading database ... 89747 files and directories currently installed.)
Preparing to unpack .../language-pack-zh-hans-base_1%3a16.04+20160627_all.deb ...
Unpacking language-pack-zh-hans-base (1:16.04+20160627) ...
Selecting previously unselected package language-pack-zh-hans.
Preparing to unpack .../language-pack-zh-hans_1%3a16.04+20160627_all.deb ...
Unpacking language-pack-zh-hans (1:16.04+20160627) ...
Setting up language-pack-zh-hans (1:16.04+20160627) ...
Setting up language-pack-zh-hans-base (1:16.04+20160627) ...
Generating locales (this might take a while)...
zh_CN.UTF-8... done
zh_SG.UTF-8... done
Generation complete.
Answer by Drew Knight
Note that this function will not work with the ‘C’ locale, so you have to set a
locale via setlocale() first.,If the given encoding is not known, the function defaults to the default
encoding for the locale code just like setlocale().,The locale module defines the following exception and functions:,If locale is omitted or None, the current setting for category is
returned.
import locale
locale.setlocale(locale.LC_ALL, '')
Answer by Nathanael Velazquez
I’m new to python and getting this following error. FYI, I’m from India so, is there any issue in Indian Date Format?,Doing some testing now. Thanks for the logs @bobpullen,
Sorry, something went wrong.
,Try this simple python program, does it also give you an error:
import locale
locale.setlocale(locale.LC_ALL, 'en_GB')
If you see the following error while installing pip:
Traceback (most recent call last):
File "/usr/bin/pip", line 11, in <module>
sys.exit(main())
File "/usr/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 581, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting
this means the environment variable LC_ALL
is missing or invalid somehow.
FIX :
run the following command:
and retry installing again.
What is LC_ALL?
LC_ALL
is the environment variable that overrides the value of the LANG
and the values of any other LC_*
environment variables.
In a script, if you want to force a specific setting, as you don’t know what settings the user has forced, your safest and generally only option is to force LC_ALL.
The C
locale is for computers. In the C
locale, characters are single bytes, the charset is ASCII, the sorting order is based on the byte values, the language is usually US English.
You generally run a command with LC_ALL=C
to avoid the user’s settings to interfere with your script. For example, if you want [a-z]
to match the 26 ASCII characters from a
to z
, you have to set LC_ALL=C
.
Hope this helps!