5

I made a small application that prints unicode special characters(i.e. superscript, subscript...). When it runs locally there are no problems but when it runs in a ssh session I always get a UnicodeEncodeError. Specifically: UnicodeEncodeError 'ascii' can't encode characters in position 0-1: ordinal not in range(128) I tried different ssh clients, computers and double checked the sessions encoding but the result is the same. This is really weird. Why does this happen? Is this really related to ssh?

12
  • Most likely because Python cannot determine the terminal encoding, and falls back to ASCII. See the docs.python.org/3/howto/unicode.html python Unicode Howto. Commented Jun 28, 2013 at 22:25
  • 2
    Does python your_script.py | cat work locally? Try PYTHONIOENCODING=utf-8 python your_script.py | cat. Commented Jun 28, 2013 at 22:35
  • @J.F.Sebastian Yes, is working. Even without that enviroment variable set. Commented Jun 28, 2013 at 22:38
  • @J.F.Sebastian With PYTHONIOENCODING=utf-8 it also work in ssh. Thanks. Commented Jun 28, 2013 at 22:46
  • 1
    what does test_io_encoding.py print? Commented Jul 1, 2013 at 4:51

1 Answer 1

2

The problem might be not your Python code, check your ssh ENV. LANG should be en_US.UTF-8 (containing UTF-8) not ASCII

Sign up to request clarification or add additional context in comments.

1 Comment

LANG is set to utf-8. Setting PYTHONIOENCODING=utf-8 will solve the issue but the strange thing is that sys.getdefaultencoding() already return utf-8. So this shouldn't change anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.