Locale.Error: Unsupported Locale Setting in Python

Locale.Error: Unsupported Locale Setting in Python

  1. What Is the Locale Module in Python
  2. What Is the locale.Error: unsupported locale setting in Python
  3. How to Fix the locale.Error: unsupported locale setting in Python
  4. Fix the locale.Error: unsupported locale setting With the export Command
  5. Fix the locale.Error: unsupported locale setting From Your Terminal
  6. Enlist All the Available Languages in the Locale Module

Python is a diverse and powerful programming language with many libraries and frameworks that allow you to achieve the desired tasks efficiently.

Regarding taking care of developers, Python is always on the top. Here is one of the famous modules to help developers generalize the software without facing any cultural barriers, and that module is Locale.

What Is the Locale Module in Python

As discussed, the locale module is developed to facilitate the developers to deal with certain cultural issues in the software.

So let’s explore the Locale module and try to fix one of the most common errors, locale.Error: unsupported locale setting you will encounter when you are new to this module.

Before going into the details of the error, let’s see what the locale module is, how to import it, and what else is required in this module.

Code example:

import locale

# get the current locale
print(locale.getlocale())

Output:

('English_United States', '1252')

We have English_United States.1252 as the preferred locale in our case; basically, it depends on the settings; you might have a different preferred locale on your machines.

But you can change the default locale into your preferred locale from the available list with the help of the setlocale() function.

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

Output:

'German_Germany.1252'

What Is the locale.Error: unsupported locale setting in Python

In Python, when you are new to the locale module, you might encounter the locale.Error: unsupported locale setting at some point. And the reasons behind that you didn’t have either properly installed the locale module or issues with the parameters you are providing.

Let’s see an example to understand the locale.Error: unsupported locale setting in a better way.

import locale
print(str(locale.getlocale()))
locale.setlocale(locale.LC_ALL, 'de_DE')

Output:

locale.Error: unsupported locale setting

And the core reason behind this error is that your environment variable LC_ALL is missing or invalid. In this case, de_DE is missing, so you get the error locale.Error: unsupported locale setting.

How to Fix the locale.Error: unsupported locale setting in Python

As we have seen in the above code, it has caused the locale error, and the reason was we were missing the environment variables, or the provided one was invalid. And to fix that, there are multiple solutions; each is explained one by one, so make sure to check out each to fix the locale.Error: unsupported locale setting.

Let’s begin with setting the environment variables. To do so, go to your terminal and type the following commands.

Fix the locale.Error: unsupported locale setting With the export Command

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

You can also do it in one line of code. Both work the same.

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

In the above commands, the LC_ALL and LC_CTYPE are used to set the environment variables, and the last command sudo dpkg-reconfigure locales is used to commit the changes into the system.

Fix the locale.Error: unsupported locale setting From Your Terminal

If that didn’t work for you, you could try to reinstall locale from your terminal.

sudo apt-get install locales -y

The above command will install locale. Now generate a list of locales with the locale-gen command.

sudo locale-gen en_US.UTF-8

And finally, set the configuration permanently to the system.

sudo echo "LANG=en_US.UTF-8" > /etc/default/locale

Running the above commands might ask you to restart your machine; you should allow it to restart.

Enlist All the Available Languages in the Locale Module

You can run the below command or the Python program to verify that the given locale exists in the locale list.

$ locale -a

Below is the Python program to see the list of available locales.

import locale
for language in locale.windows_locale.values():
    print(language, end =", ")

Output:

af_ZA, sq_AL, gsw_FR, am_ET, de_DE, de_CH, ....., sah_RU, ii_CN, yo_NG, zu_ZA

The above program will loop through the available list of locale languages and print each as shown in the output. Now you can pick anything available in the list and put it in the program to see its output, which should work properly.

Code Example:

import locale
print(str(locale.getlocale()))
locale.setlocale(locale.LC_ALL, 'de_DE')

Output:

('de_DE', 'UTF-8')
'de_DE'

Perfect! As you can see, it is working perfectly; we have set the locale language as de_DE as it’s running smoothly.

Remember de_DE exists in the list of the local languages, as shown in the above example, and it represents the German language.

Zeeshan Afridi avatar Zeeshan Afridi avatar

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

LinkedIn

Related Article - Python Error

  • Python PermissionError: [WinError 5] Access Is Denied
  • Python TypeError: 'DataFrame' Object Is Not Callable
  • Python TypeError: Can't Convert 'List' Object to STR
  • Local Variable Referenced Before Assignment Error in Python
  • Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
  • Python Is Not Recognized in Windows 10