How to Fix “locale.error: unsupported locale setting”?

A locale comprises a set of basic guidelines on how the time and date are displayed on your computer. If any application/program does not support/accept the machine’s locale settings, you may encounter a “locale.error: unsupported locale setting” in Python. This occurs due to the absence of the locale module or the invalid environment variable.

This post will list the possible reasons and solutions to fix the above-stated error with the following guideline. 

Reason: Missing Environment Variable

The main reason for this error is that the environment variable “LC_ALL” is missing or invalid. The following snippet shows the import of the locale module in the Python shell:

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

In the above example environment variable “LC_ALL” is missing. 

Solution 1: Setting Up Environment Variable in Current Session

To resolve the error, add an environment variable temporarily to address the locale settings.

For this, open the terminal and set the environment variables with the help of the “export” command as shown below:

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

Enter the root user password to continue the process:

Here, the “Configuration locales” window pops up. Press the “Enter” key to set the default locale “en_US.UTF-8”:

The “en_US.UTF-8” locale is selected and configured permanently. Again press the “Enter” key, and the locales generation list down all the available formats configured:

Solution 2: Configure the Locale Setting Permanently

This solution will add the locale setting’s module by adding the environment variable permanently.

Step 1: Install the Locale Module

First, log in as the root user because the installation requires root user privileges. Type the below “apt” command on the terminal to install the “locale” module:

# yum install locales -y                                                               #For CentOS/RHEL
# dnf install locales -y                                                                #For Fedora
# apt install locales -y                                                                #For Ubuntu/Debian-Based

The “locale” module is installed successfully.

Step 2: Create the Locale List

Now, create the list of “locale” modules by running the following “locale-gen” command:

# locale-gen en_US.UTF-8

The output verifies that the “Generation” is completed.

Step 3: Maintain the Configuration Permanently

The last step is to set the configurations permanently to the system to avoid the above error.

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

The above command may be asked to reboot the system. The best approach is to restart the system for better results.

Conclusion

The “locale.error: unsupported locale setting” occurs due to the “missing environment variables or passing invalid parameters”. To resolve this, “Set the Environment Variables” via the “export” command or configure the locale setting permanently. This post has illustrated all possible reasons and workable solutions to the error “ locale.error: unsupported locale setting”.