setlocale stopped working in lambda python 3.12
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.
- Topics
- ServerlessCompute
- Tags
- AWS Lambda
- Language
- English
asked 2 years ago944 views
- Newest
- Most votes
- Most comments
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
You could put it into your lambda layers
answered a year ago
- Krisztian Pintera year ago
it appears that you don't even need layers. you can copy the files from the
/usr/lib/localedirectory 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 usingos.environ["LOCPATH"] = ...before callingsetlocale. it even seems to be architecture independent, e.g. i used x86 to install locales, then used it from an arm lambda.
one can use the pyuca module instead. it is pure python.
answered a year ago
