I recently wrote a Python script on a server and ran it remotely from my local machine running macOS 10.14:
1 | ssh knktc@cloud.knktc.com /home/knktc/test.py |
But it failed with an error like this:
1 | UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 1941: ordinal not in range(128) |
Since everything had already moved to Python 3, seeing an encoding problem like this felt a bit odd.
I checked the local LC_CTYPE with locale and saw it was zh_CN.UTF-8. Then I ran locale -a on the remote server and found that the server did not support zh_CN.UTF-8.
So the likely issue was that the local SSH client was forwarding locale environment variables, but the remote system could not use them properly and ended up falling back to ASCII.
To fix it, you can either change the local SSH client configuration or the remote SSH server configuration.
Option 1: edit the local SSH client config at /etc/ssh/ssh_config and comment out:
1 | SendEnv LANG LC_* |
Option 2: edit the remote server’s /etc/ssh/sshd_config and comment out:
1 | AcceptEnv LANG LC_* |
If you change the server config, restart sshd afterwards.
After making one of those changes, the remote Python script runs normally again.