alive-progress: UnicodeEncodeError on some Linux'es (ubuntu 16.04 and Slackware 14.2)

In [17]: from alive_progress import alive_bar

In [18]: def pretty_sleep(sleep_time: int, bar='filling'):
    ...:     """pretty sleep with progress bar"""
    ...:     with alive_bar(int(sleep_time), bar=bar) as bar:
    ...:         for i in range(int(sleep_time)):
    ...:             time.sleep(0.001)
    ...:             bar()
    ...:

In [19]: pretty_sleep(100)
Exception in thread Thread-1851:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/home/rjosyula/x/lib/python3.5/site-packages/alive_progress/progress.py", line 100, in run
    alive_repr(next(player))
  File "/home/rjosyula/x/lib/python3.5/site-packages/alive_progress/progress.py", line 115, in alive_repr
    sys.__stdout__.write(line + (spin and '\r' or '\n'))
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 43-45: ordinal not in range(256)

---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-19-5b2d8b0fde03> in <module>
----> 1 pretty_sleep(100)

<ipython-input-18-920a2b64dd16> in pretty_sleep(sleep_time, bar)
      4         for i in range(int(sleep_time)):
      5             time.sleep(0.001)
----> 6             bar()
      7

/usr/lib/python3.5/contextlib.py in __exit__(self, type, value, traceback)
     64         if type is None:
     65             try:
---> 66                 next(self.gen)
     67             except StopIteration:
     68                 return

~/x/lib/python3.5/site-packages/alive_progress/progress.py in alive_bar(total, title, calibrate, **options)
    257
    258     end, run.text, run.stats = True, '', stats_end
--> 259     alive_repr()

~/x/lib/python3.5/site-packages/alive_progress/progress.py in alive_repr(spin)
    113             if line_len < run.last_line_len:
    114                 clear_traces()
--> 115             sys.__stdout__.write(line + (spin and '\r' or '\n'))
    116             sys.__stdout__.flush()
    117

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 1-40: ordinal not in range(256)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 22 (11 by maintainers)

Most upvoted comments

@TheTechRobo: the problem is that when code is distributed there’s no guarantee regarding the environment that is going to be used.

This is not an issue for my particular case because users of my code never start Python directly, but rather via a wrapper script. So I have the freedom to set the environment in the wrapper script and be done with it.

Others might want to write their own applications/modules using alive-progress. When they distribute the module they have no control over the environment where it’s going to be used.

A simple warning regarding this potential issue in alive_progress documentation could suffice. A better approach would be to try to catch the non-UTF-8 encoding and default to a “classic” progress bar appearance. But this is up to @rsalmei since he’s the maintainer.

I just added bar='classic' as well, after realising that these are two different configurations. Using both of these, the progress bar works perfectly as the classic settings.

Hey @MurdoMaclachlan, sorry I took so long, haven’t seen your reply!

Yeah, it’s just a newbie mistake, there’s a spinner named classic, but not a theme. Just change word theme -> spinner.

Could you please test something? Run this in your shell and try again: PYTHONIOENCODING=utf8

There’re some other tricks here if that doesn’t work: https://stackoverflow.com/questions/2276200/changing-default-encoding-of-python

What does your system report to this?

import sys
sys.getdefaultencoding()