Skip to content

Exception thrown when output is not configured as utf-8 #212

@DonBraulio

Description

@DonBraulio

Describe the bug
Running a program with a rich progressbar inside a docker container, I got the following exception:

  File "/usr/local/lib/python3.6/dist-packages/rich/progress.py", line 150, in track
    sequence, total=total, description=description, update_period=update_period
  File "/usr/local/lib/python3.6/dist-packages/rich/progress.py", line 667, in track
    with self:
  File "/usr/local/lib/python3.6/dist-packages/rich/progress.py", line 627, in __enter__
    self.start()
  File "/usr/local/lib/python3.6/dist-packages/rich/progress.py", line 596, in start
    self.refresh()
  File "/usr/local/lib/python3.6/dist-packages/rich/progress.py", line 812, in refresh
    self.console.print(Control(""))
  File "/usr/local/lib/python3.6/dist-packages/rich/console.py", line 448, in __exit__
    self._exit_buffer()
  File "/usr/local/lib/python3.6/dist-packages/rich/console.py", line 426, in _exit_buffer
    self._check_buffer()
  File "/usr/local/lib/python3.6/dist-packages/rich/console.py", line 993, in _check_buffer
    self.file.write(text)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-55: ordinal not in range(128)

The problem can be easily solved in my case by setting the encoding properly:

export PYTHONIOENCODING=utf8

After that, the script runs perfectly fine with the beautiful rich progressbar.

However, setting this env var in some environments might not be possible or have other undesired consequences. It would be great if there was some kind of failback mode, or a WARNING with a suggestion to set this var, instead of failing without further information.

To Reproduce
Use the README progressbar example, let's call this file progress.py:

from rich.progress import track
for step in track(range(100)):
    pass

Run it with ascii encoding vs. utf8 and check the difference:

PYTHONIOENCODING=ascii python progress.py  # Exception thrown
PYTHONIOENCODING=utf8 python progress.py  # Works

Platform

  • rich==5.0.0
  • Probably works on any python3 installation. Reproduced on Python 3.6.10 on OSX Catalina, and Python 3.6.9 on Ubuntu Linux native and the same version on Linux for tegra (ARM) docker image.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions