Question

I'm not sure if this a good place to ask for help with this error. Somehow I seem not to have a locale on my Debian Linux system. Basically, I became aware of this when a python program I was trying to run executed the line locale.setlocale(locale.LC_ALL, 'en_US'). I get the error:

Traceback (most recent call last):
File "", line 1, in
File "runserver.py", line 4, in
site = TarbellSite(os.path.dirname(os.path.abspath(file)))
File "/home/brian/.virtualenvs/tarbell/src/flask-tarbell/tarbell/app.py", line 36, in init
self.projects = self.load_projects()
File "/home/brian/.virtualenvs/tarbell/src/flask-tarbell/tarbell/app.py", line 59, in load_projects
project = imp.load_module(name, filename, pathname, description)
File "/home/brian/Code/contrib/tarbell/base/config.py", line 28, in
locale.setlocale(locale.LC_ALL, 'en_US')
File "/home/brian/.virtualenvs/tarbell/lib/python2.7/locale.py", line 547, in setlocale
return _setlocale(category, locale)
locale.Error: unsupported locale setting

However, researching relevant extant questions on SO like this one, tells me to run locale -a, which yields:

C
C.UTF-8
en_US.utf8
POSIX

So, it looks like US english IS in the locales on my system. What am I doing wrong? I am running into various other road blocks. sudo apt-get install language-pack-en according to a lot of places on the internet gets the english language pack. But apt complains that this package doesn't exist. How do I get this damn locale?

Was it helpful?

Solution

try to add the 'utf8' bit as well;

import locale
locale.getlocale()
>>> (None, None)
locale.setlocale(locale.LC_ALL, 'en_US.utf8')
>>> 'en_US.utf8'
locale.getlocale()
>>> ('en_US', 'UTF-8')

OTHER TIPS

This is the solution that worked for me:

I am using an iMac with OS X 10.11.6 (15G19009) Python version 3.5.2

I tried:

locale.setlocale(locale.LC_ALL,'') <-- this line worked fine on windows 7

The Solution for the MAC was to specify English like this:

locale.setlocale(locale.LC_ALL,'EN_US') <-- this line produced the desired results

These two lines must be os independent. after further testing this is a cross platform solution, you can adapt this to any platform by simply calling platform.platform and making the necessary changes.

import locale
import platform
if (platform.platform(aliased = 0, terse = 0)=='Darwin-15.6.0-x86_64-i386-64bit'):
    locale.setlocale( locale.LC_ALL, 'EN_US' )
else:
    locale.setlocale(locale.LC_ALL,'')

This is a more eloquent answer:

import platform
import locale
system = platform.system() ##<-- to get the platform you are using.
if (system == 'Darwin'): ##<-- Darwin has Different naming
    locale.setlocale(locale.LC_ALL, 'EN_US') ##<-- Default us english on Darwin
else: ##<-- if system isn't Darwin 
    locale.setlocale(locale.LC_ALL,'') ##<-- this works for Windows and Debian
## operations like locale.currency ect......

This code allows for multi-platform development the error displayed in this post was improper setting of locale. This answer allows for at least 3 different platforms to set locale correctly using only standard modules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top