question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception thrown when output is not configured as utf-8

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
willmcgugancommented, Aug 10, 2020

This is what happens if you get a unicode error now:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-75: ordinal not in range(128)
*** You may need to add PYTHONIOENCODING=utf-8 to your environment ***

Hopefully that will save time for developers running Rich code in docker / CI etc.

Still considering other options for this issue…

2reactions
willmcgugancommented, Aug 6, 2020

Catching the exception will be easy. There is only a single line in Console that actually writes to stdout. I can catch the exception there and re-raise it with some helpful info.

There’s not much I can do to avoid it entirely that doesn’t potentially break something else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exception thrown when output is not configured as utf-8 #212
My understanding is that when Python doesn't have a LANG env var set, it will default to ascii encoding. But Rich can't know...
Read more >
The encoding 'UTF-8' is not supported by the Java runtime
Strange. UTF-8 is a required encoding. No one should release a Java runtime that doesn't include UTF-8 and US-ASCII.
Read more >
UTF8Encoding Class (System.Text) - Microsoft Learn
Represents a UTF-8 encoding of Unicode characters. ... Without error detection, no exception is thrown, and the invalid sequence is generally ignored.
Read more >
UTF8-CPP: UTF-8 with C++ in a Portable Way
A simple, portable and lightweigt C++ library for easy handling of UTF-8 encoded strings.
Read more >
FAQ - UTF-8, UTF-16, UTF-32 & BOM - Unicode
Q: Are there any byte sequences that are not generated by a UTF? ... Unicode conformance requires that encoding form conversion always results...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found