Locale error: unsupported locale setting

Hi,

Everything works fine in my local environment. However, I am getting a locale error: unsupported locale setting error on deploying to Streamlit cloud. I am setting the locale as follows -

locale.setlocale(locale.LC_ALL, "en_US")

Any ideas?

Thanks

Hey @mjo,

Thanks for sharing your question! Can you share a link to the GitHub repo so we can try to reproduce this issue?

Hey,
I’m getting a similar issue when attempting to set the locale on Streamlit cloud.
Steps to produce:

import locale
def format_money(res: int) -> str:
    """
    Given some int, convert it to currency
    Args:
        res (int): Money input as int
    Returns:
        str: Currency as string
    """
    locale.setlocale(locale.LC_ALL, "")
    return locale.currency(res, grouping=True)
money = format_money(2)

Expected Result $2.00
Actual Result ValueError: Currency formatting is not possible using the 'C' locale.

Hi @bryce,

Welcome to the community! :wave: :partying_face:

I ran into the error locally too (not Cloud-specific):

The solution according to this StackOverflow post is to use instead:

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

Your code would need to be modified to the following:

import locale
import streamlit as st

def format_money(res: int) -> str:
    """
    Given some int, convert it to currency
    Args:
        res (int): Money input as int
    Returns:
        str: Currency as string
    """
    locale.setlocale(locale.LC_ALL, "en_CA.UTF-8")
    return locale.currency(res, grouping=True)


money = format_money(2)

st.write(money)

Happy Streamlit-ing! :balloon:

Hey @snehankekre, did you confirm that this works in Streamlit cloud? I actually tried the same encoding there (‘en_CA.UTF-8’) as well, to no avail.

Messing with the locale module has always been a pain for me. Why not just f"${res:.2f}"?

1 Like

Hey @bryce, good catch, I hadn’t!

To get this working on Community Cloud, create a packages.txt file in your repo containing:

locales
locales-all

Then the following code should work:

import locale
import streamlit as st

def format_money(res: int) -> str:
    """
    Given some int, convert it to currency
    Args:
        res (int): Money input as int
    Returns:
        str: Currency as string
    """
    locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
    return locale.currency(res, grouping=True)


money = format_money(2)

st.write(money)

The packages.txt file contains debian dependencies that will be installed in your app container via apt-get install. Precompiled locale data usually does not exist on fresh systems (new debian containers). We install them via packages.txt:

1 Like

Also receiving a similar error even though there’s a package containing locales
locales-all. Only seems to be a cloud specific issue.

Has any one found a solution to this any help would be much appreciated

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.