Python cannot determine the locale used when inside a snap

I’m developing a python app using the urwid library to build the user interface. I’m facing an issue where users cannot type or paste Unicode characters inside my app.

After some investigation I found that this works when launching the code before packaging it as a snap, but not after. I’ve noticed that, from within the snap, Python doesn’t seem to find the right locale:

$ snap run --shell my_snap
$ /usr/bin/python3 
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getlocale()
(None, None)

However, from my usual session:

Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getlocale()
('en_US', 'UTF-8')

I tried to check the locale from env, but it seems it’s the same no matter if I’m inside or outside the snap:

From within the snap:

$ env | grep LC_ | sort
LC_ADDRESS=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_MONETARY=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_NUMERIC=en_US.UTF-8
LC_PAPER=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_TIME=en_US.UTF-8
$ env | grep LANG
LANG=en_US.UTF-8
$ exit

From outside of the snap:

$ env | grep LC_ | sort
LC_ADDRESS=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_MONETARY=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_NUMERIC=en_US.UTF-8
LC_PAPER=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_TIME=en_US.UTF-8
$ env | grep LANG
LANG=en_US.UTF-8

How can I make sure that Python receives the right information regarding the locale used (and ultimately, I can use Unicode with urwid)?

Check out The gettext-launch remote part - doc - snapcraft.io and The locales-launch remote part - doc - snapcraft.io.

Thanks a lot @Lin-Buo-Ren! I added:

    environment:
      LANG: C.UTF-8
      LC_ALL: C.UTF-8

to my app’s part and it seems it was enough for Python (and therefore urwid) to take Unicode into account. I can’t reproduce my issue.

1 Like