$ PYTHONIOENCODING=utf-8 python echo_unicode.py
☃
$ PYTHONIOENCODING=latin1 python echo_unicode.py
?
$ PYTHONIOENCODING=utf-8 python3 echo_unicode.py
☃
$ PYTHONIOENCODING=latin1 python3 echo_unicode.py
Traceback (most recent call last):
File "echo_unicode.py", line 8, in <module>
echo_unicode()
File "/home/jwodder/tmp/venv3/lib/python3.5/site-packages/click/core.py", line
716, in __call__
return self.main(*args, **kwargs)
File "/home/jwodder/tmp/venv3/lib/python3.5/site-packages/click/core.py", line
696, in main
rv = self.invoke(ctx)
File "/home/jwodder/tmp/venv3/lib/python3.5/site-packages/click/core.py", line
889, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/jwodder/tmp/venv3/lib/python3.5/site-packages/click/core.py", line 534, in invoke
return callback(*args, **kwargs)
File "clack.py", line 5, in echo_unicode
click.echo(u'\u2603')
File "/home/jwodder/tmp/venv3/lib/python3.5/site-packages/click/utils.py", line 259, in echo
file.write(message)
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2603' in position 0: ordinal not in range(256)
When printing text with
click.echo
, Click applies the'replace'
error handler in Python 2 but not in Python 3.Consider this program:
When run: