UnicodeDecodeError when ssh from OS X
<p>My Django app loads some files on startup (or when I execute management command). When I ssh from one of my Arch or Ubuntu machines all works fine, I am able to successfully run any commands and migrations.</p>

<p>But when I ssh from OS X (I have El Capital) and try to do same things I get this error: </p>

<pre><code>UnicodeDecodeError: 'ASCII' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
</code></pre>

<p>To open my files I use <code>with open(path_to_file) as f: ...</code></p>

<p>The error happens when sshing from both iterm and terminal. I found out that reason was <code>LC_CTYPE</code> environment variable. It wasn't set on my other Linux machines but on mac it was <code>UTF-8</code> so after I ssh to the server it was set the same. The error was fixed after I unset <code>LC_CTYPE</code>.</p>

<p>So the actual question is what has happened and how to avoid this further? I can unset this variable in my local machine but will it take some negative effects? And what is the best way of doing this?</p>

<p>Your terminal at your local machine uses a character encoding. The encoding it uses appears to be UTF-8. When you log on to your server (BTW, what OS does it run?) the programs that run there need to know what encoding your terminal supports so that they display stuff as needed. They get this information from <code>LC_CTYPE</code>. <code>ssh</code> correctly sets it to UTF-8, because that's what your terminal supports.</p>

<p>When you unset <code>LC_CTYPE</code>, then your programs use the default, <code>ASCII</code>. The programs now display in <code>ASCII</code> instead of <code>UTF-8</code>, which works because <code>UTF-8</code> is backward compatible with <code>ASCII</code>. However, if a program needs to display a special character that does not exist in <code>ASCII</code>, it won't work.</p>

<p>Although from the information you give it's not entirely clear to me why the system behaves in this way, I can tell you that unsetting <code>LC_CTYPE</code> is a bad workaround. To avoid problems in the future, it would be better to make sure that all your terminals in all your machines use UTF-8, and get rid of ASCII.</p>

<p>When you try to <code>open</code> a file, Python uses the terminal's (i.e. <code>LC_CTYPE</code>'s) character set. I've never quite understood why it's made this way; why should the character set of your terminal indicate the encoding a file has? However, that's the way it's made and the way to fix the problem correctly is to use the <code>encoding</code> parameter of <code>open</code> if you are using Python 3, or the <code>codecs</code> standard library module if you are using Python 2.</p>

python django osx ssh locale