Skip to content

setlocale stopped working in lambda python 3.12

0

apparently between 3.11 and 3.12, locale support was removed?

this code:

import locale

def attempt(c, l=None):
    try:
        loc = locale.setlocale(c, l)
    except Exception as e:
        loc = f"{e}"
    print(f"{c} {l} -> {loc}")

def lambda_handler(event, context):
    attempt(locale.LC_COLLATE)
    attempt(locale.LC_ALL, '')
    attempt(locale.LC_COLLATE)
    attempt(locale.LC_COLLATE, 'en_US.UTF-8')
    attempt(locale.LC_COLLATE, 'hu_HU.UTF-8')

returns this on 3.11:

3 None -> C
6  -> en_US.UTF-8
3 None -> en_US.UTF-8
3 en_US.UTF-8 -> en_US.UTF-8
3 hu_HU.UTF-8 -> hu_HU.UTF-8

but this on 3.12:

3 None -> C
6  -> unsupported locale setting
3 None -> C
3 en_US.UTF-8 -> unsupported locale setting
3 hu_HU.UTF-8 -> unsupported locale setting

it also works locally.

asked 2 years ago832 views
3 Answers
0

Hello.

Starting with Python 3.12, the Lambda OS is now Amazon Linux 2023.
I think this is probably related.
https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html#runtimes-supported

There is also a report on stackoverflow, but it seems that it is necessary to install "glibc-all-langpacks" for Amazon Linux 2023 containers.
https://stackoverflow.com/questions/76581344/cannot-set-installed-locale-with-aws-linux-2023

You can check the packages installed on Amazon Linux 2023 in the following document.
https://docs.aws.amazon.com/AL2/latest/relnotes/relnotes-20230424.html

If you really want to change the locale settings, you may need to use a custom runtime.
https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html
https://docs.aws.amazon.com/lambda/latest/dg/images-create.html

EXPERT
answered 2 years ago
EXPERT
reviewed 2 years ago
  • disappointing. i don't think a custom runtime solves this, if it involves dnf. it seems that i'll need to containerize. it is really weird to me that aws would go with such a regression.

0
  • it appears that you don't even need layers. you can copy the files from the /usr/lib/locale directory of an AL2023 instance, add to the zip package, and set the environment variable LOCPATH to it. you can set from the lambda environment, or just from code using os.environ["LOCPATH"] = ... before calling setlocale. it even seems to be architecture independent, e.g. i used x86 to install locales, then used it from an arm lambda.

0

one can use the pyuca module instead. it is pure python.

answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.