What is the correct way to set Python's locale on Windows?

113,144

Solution 1

It seems you're using Windows. The locale strings are different there. Take a more precise look at the doc:

locale.setlocale(locale.LC_ALL, 'de_DE') # use German locale; name might vary with platform

On Windows, I think it would be something like:

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

MSDN has a list of language strings and of country/region strings

Solution 2

You should not pass an explicit locale to setlocale, it is wrong. Let it find out from the environment. You have to pass it an empty string

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

Solution 3

This is the only correct way to use it, providing an example for the German locale:

import locale

locale.setlocale(
    category=locale.LC_ALL,
    locale="German"  # Note: do not use "de_DE" as it doesn't work
)

Solution 4

Ubuntu

On Ubuntu you may have this problem because you don't have that local installed on your system.

From shell try a:

$> locale -a

and check if you find the locale you are interested in. Otherwise you have to install it:

$> sudo apt-get install language-pack-XXX

where XXX is your language (in my case "xxx = it" , italian locale) Then run a dpkg-reconfigure:

$> sudo dpkg-reconfigure locales

After that try again in your python shell:

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

(this is for italian locale, which was what I needed)

Solution 5

From locale.setlocale docs:

locale.setlocale(category, locale=None):
    """
    Set the locale for the given category.  The locale can be
    a string, an iterable of two strings (language code and encoding),
    or None.
    """"

Under Linux (especially Ubuntu) you can either use

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

or

locale.setlocale(locale.LC_ALL, ('de', 'utf-8'))

You will get the same error if the locale is not installed on the system. So, make sure you have the locale installed on your system:

$ locale -a # to list the currently installed locales
$ (sudo) locale-gen de_DE.UTF-8 # to install new locale
Share:
113,144

Related videos on Youtube

DNS
Author by

DNS

Among other things, I currently maintain the Flot plotting/charting library. If you're interested in attractive JavaScript plotting for jQuery, please take a look at http://www.flotcharts.org.

Updated on November 09, 2021

Comments

  • DNS
    DNS over 2 years

    I'm attempting to sort a list of strings in a locale-aware manner. I've used the Babel library for other i18n-related tasks, but it doesn't support sorting. Python's locale module provides a strcoll function, but requires the locale of the process to be set to the one I want to work with. Kind of a pain, but I can live with it.

    The problem is that I can't seem to actually set the locale. The documentation for the locale module gives this example:

    import locale
    locale.setlocale(locale.LC_ALL, 'de_DE')
    

    When I run that, I get this:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Python26\Lib\locale.py", line 494, in setlocale
    locale.Error: unsupported locale setting
    

    What am I doing wrong?

  • DNS
    DNS over 14 years
    I didn't mention this in the question because it wasn't directly related, but the code I was writing at the time was designed for use on a web server. In other words, the locale might change with each request, and is not necessarily ever the same as the environment's locale.
  • u0b34a0f6ae
    u0b34a0f6ae over 14 years
    DNS: Have you read the docs for locale? It implies it might be dangerous to call setlocale "much", and it is not thread safe. So perhaps something else than setlocale is the solution. Gettext can load different catalogs and switch at runtime for example; but I don't know what you are using the locale for.
  • Zoneur
    Zoneur over 10 years
    FWIW, I had the problem under Ubuntu 13.04, Linux 3.8.0-19, with python 2.7.4 when trying to set the locale to fr_FR. Setting it to fr_FR.UTF-8 worked for me.
  • Christoph
    Christoph over 7 years
    This question asks specifically about Windows
  • srodriguex
    srodriguex about 6 years
    For Python 3.6.3 in Conda 4.4.11, Windows 7, the locale strings seem to be the same as other OS.
  • Cristian Ciupitu
    Cristian Ciupitu almost 6 years
    This list is more comprehensive: msdn.microsoft.com/en-us/library/cc233982.aspx
  • ImportanceOfBeingErnest
    ImportanceOfBeingErnest almost 6 years
    This is the only answer that actually answers the question.
  • Gabriel
    Gabriel over 5 years
    Unless your user is not on your local machine
  • Boop
    Boop almost 5 years
    The linked docs seem to be too recent for WServer2018R2 I had to do use 'eng_usa' ('en_US' didn't work)
  • Henrique Brisola
    Henrique Brisola almost 4 years
    this helped me, but I had to change German to Portuguese_Brazil.1252. Do you know a documentation listing these values?