aegirhall / console-menu

A simple Python menu system for building terminal user interfaces.
MIT License
366 stars 58 forks source link

UnicodeEncodeError in linux for printing menu box #38

Closed renanGit closed 4 years ago

renanGit commented 4 years ago

I dont know if this is something worth fixing, but I just wanted it to be known.

I pasted the example4.py in my environment in linux (SLES11) and every time I run it, this UnicodeEncodeError comes up.

Just FYI I'm covering up the path with '*' below.

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/*/python/envs/3.6.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/*/python/envs/3.6.6/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/*/consolemenu_test/vendor/lib/python3.6/site-packages/consolemenu/console_menu.py", line 169, in _wrap_start
    self._main_loop()
  File "/*/consolemenu_test/vendor/lib/python3.6/site-packages/consolemenu/console_menu.py", line 225, in _main_loop
    self.draw()
  File "/*/consolemenu_test/vendor/lib/python3.6/site-packages/consolemenu/console_menu.py", line 233, in draw
    prologue_text=self.prologue_text, epilogue_text=self.epilogue_text))
  File "/*/consolemenu_test/vendor/lib/python3.6/site-packages/consolemenu/screen.py", line 74, in printf
    print(*args, end='')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-77: ordinal not in range(128)

I found out that the sys.stdout for my linux python is ANSI_X3.4-1968 and windows is utf-8. I tried many different ways, but after changing my encoding to utf-8 it at least works.

import sys, codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
renanGit commented 4 years ago

Reference I used https://wiki.python.org/moin/PrintFails

renanGit commented 4 years ago

Issue was resolved by changing the Konsole character encoding from Western European ISO 8859-1 -> UTF-8 None of the export LANG suggestions worked, just FYI.