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 24 days ago143 views
1 Answer
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

profile picture
EXPERT
answered 24 days ago
profile pictureAWS
EXPERT
reviewed 24 days 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.

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.

Guidelines for Answering Questions