printing UTF-8 in Python 3 using Sublime Text 3
<p>I have this Python3 code to attempt to read and print from a utf-8 encoded file:</p>

<pre><code>f = open('mybook.txt', encoding='utf-8')
for line in f:
    print(line)
</code></pre>

<p>When I build using Sublime Text 3 I get the following error:</p>

<pre><code>UnicodeEncodeError: 'ascii' codec can't encode character '\u2019' in position 18: ordinal not in range(128)
</code></pre>

<p>However, it works file when I just execute my code in the terminal with python3.</p>

<p>My build configuration is</p>

<pre><code>{
"cmd": ["/usr/local/bin/python3", "$file"]
, "selector": "source.python"
, "file_regex": "file \"(...*?)\", line ([0-9]+)"
}
</code></pre>

<p>If I change it to:</p>

<pre><code>f = open('mybook.txt', encoding='utf-8')
for line in f:
    print(line.encode('utf-8'))
</code></pre>

<p>Then it does print the utf-8 encoded byte string (I think that's what's happening).</p>

<pre><code>b'Hello\n'
b'\xc2\xab\xe2\x80\xa2\n'
b'Goodbye'
</code></pre>

<p>But I also don't know how to go from this to printing the unicode characters on the screen...</p>

<p>Also, if I try changing this env variable as per <a href="http://stackoverflow.com/questions/34302902/a-python-program-fails-to-execute-in-sublime-text-3-but-success-in-bash">A python program fails to execute in sublime text 3, but success in bash</a> it still doesn't fix it.</p>

<p>The answer was actually in the question linked in your question - <code>PYTHONIOENCODING</code> needs to be set to <code>"utf-8"</code>. However, since OS X is silly and doesn't pick up on environment variables set in Terminal or via <code>.bashrc</code> or similar files, this won't work in the way indicated in the answer to the other question. Instead, you need to pass that environment variable to Sublime. </p>

<p>Luckily, ST3 build systems (I don't know about ST2) have the <a href="http://docs.sublimetext.info/en/latest/reference/build_systems/exec.html#exec-command-arguments" rel="nofollow"><code>"env"</code></a> option. This is a dictionary of keys and values passed to <code>exec.py</code>, which is responsible for running build systems without the <code>"target"</code> option set. As discussed in our comments above, I indicated that your sample program worked fine on a UTF-8-encoded text file containing non-ASCII characters when run with ST3 (Build 3122) on Linux, but not with the same version run on OS X. All that was necessary to get it to run was to change the build system to enclude this line:</p>

<pre><code>"env": {"PYTHONIOENCODING": "utf8"},
</code></pre>

<p>I saved the build system, hit <kbd>â</kbd><kbd>B</kbd>, and the program ran fine.</p>

<p>BTW, if you'd like to read <code>exec.py</code>, or <code>Packages/Python/Python.sublime-build</code>, or any other file packed up in a <code>.sublime-package</code> archive, install <a href="https://packagecontrol.io/packages/PackageResourceViewer" rel="nofollow"><code>PackageResourceViewer</code></a> via Package Control. Use the "Open Resource" option in the Command Palette to pick individual files, or "Extract Package" (both are preceded by "PackageResourceViewer:", or <strong><em><code>prv</code></em></strong> using fuzzy search) to extract an entire package to your <code>Packages</code> folder, which is accessed by selecting <strong><code>Sublime Text â Preferences â Browse Packagesâ¦</code></strong> (just <strong><code>Preferences â Browse Packagesâ¦</code></strong> on other operating systems). It is located on your hard drive in the following location:</p>

<ul>
<li>Linux: <code>~/.config/sublime-text-3/Packages</code></li>
<li>OS X: <code>~/Library/Application Support/Sublime Text 3/Packages</code></li>
<li>Windows Regular Install: <code>C:\Users\<b><i>YourUserName</i></b>\AppData\Roaming\Sublime Text 3\Packages</code></li>
<li>Windows Portable Install: <code><b><i>InstallationFolder</i></b>\Sublime Text 3\Data\Packages</code></li>
</ul>

<p>Once files are saved to your <code>Packages</code> folder (if you just view them via the "Open Resource" option and close without changing or saving them, they won't be), they will override the identically-named file contained within the <code>.sublime-package</code> archive. So, for instance, if you want to edit the default <code>Python.sublime-build</code> file in the <code>Python</code> package, your changes will be saved as <code>Packages/Python/Python.sublime-build</code>, and when you choose the <code>Python</code> build system from the menu, it will only use your version.</p>

python python-3.x utf-8 sublimetext3 sublimetext