Issue with Python library (urwid) and locale encodings

Hello all!

I’m using urwid 2.1.0 in my Python project. If I try to install and launch my snap from Ubuntu 18.04 (desktop), it works fine. However, if I try to run it from Ubuntu Core 16, I get the following error:

Traceback (most recent call last):
  File "/snap/qabro/x1/bin/qabro", line 11, in <module>
    load_entry_point('qabro==0.12.dev0', 'console_scripts', 'qabro')()
  File "/snap/qabro/x1/lib/python3.6/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/snap/qabro/x1/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2852, in load_entry_point
    return ep.load()
  File "/snap/qabro/x1/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2443, in load
    return self.resolve()
  File "/snap/qabro/x1/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2449, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/snap/qabro/x1/lib/python3.6/site-packages/qabro/__init__.py", line 8, in <module>
    from qabro.ui import ReportScreen
  File "/snap/qabro/x1/lib/python3.6/site-packages/qabro/ui.py", line 2, in <module>
    import urwid
  File "/snap/qabro/x1/lib/python3.6/site-packages/urwid/__init__.py", line 26, in <module>
    from urwid.widget import (FLOW, BOX, FIXED, LEFT, RIGHT, CENTER, TOP, MIDDLE,
  File "/snap/qabro/x1/lib/python3.6/site-packages/urwid/widget.py", line 27, in <module>
    from urwid.util import (MetaSuper, decompose_tagmarkup, calc_width,
  File "/snap/qabro/x1/lib/python3.6/site-packages/urwid/util.py", line 61, in <module>
    detected_encoding = detect_encoding()
  File "/snap/qabro/x1/lib/python3.6/site-packages/urwid/util.py", line 58, in detect_encoding
    locale.setlocale(locale.LC_ALL, initial)
  File "/snap/qabro/x1/usr/lib/python3.6/locale.py", line 598, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

After looking around, I found this Github issue that seems heavily related.

I would like to know if there is any kind of workaround I could use in my snap to avoid this issue…

You can reproduce this issue easily in any UC16 container, by running this:

$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getlocale()
('en_US', 'UTF-8')
>>> initial = locale.getlocale()
>>> locale.setlocale(locale.LC_ALL, initial)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/snap/qabro/102/usr/lib/python3.6/locale.py", line 598, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

Here is the output of locale in UC16:

$ locale -a
C
C.UTF-8
POSIX

$ locale
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_NUMERIC=en_US.UTF-8
LC_TIME=en_US.UTF-8
LC_COLLATE="C.UTF-8"
LC_MONETARY=en_US.UTF-8
LC_MESSAGES="C.UTF-8"
LC_PAPER=en_US.UTF-8
LC_NAME=en_US.UTF-8
LC_ADDRESS=en_US.UTF-8
LC_TELEPHONE=en_US.UTF-8
LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=en_US.UTF-8
LC_ALL=

I’m not sure why there are some en_US.UTF-8 when this encoding is not available in locale -a

try including the locales-launch remote part in your snap, that will generate proper locales on first start:

Also refer to The locales-launch launcher: Fix Glibc locales in the snap runtime and The gettext-launch launcher: Fix gettext-based internationalization in the snap runtime .

Thanks for your answers!

I tried to get the laocales-launch to work for about an hour, and gave up.

Instead, I decided to go the lazy way and to use the urwid package from the Debian repos (python3-urwid, currently in v2.0.1) instead than package urwid from PyPI (currently in v2.1.0, causing the issue).