python and ipython seem to treat unicode characters differently
<p>I'm running python 2.7.12 from anaconda on windows 10. Included in the distro is ipython 5.1.0. I wrote a program to print certain columns of queried rows in a mysql database. The columns contain strings in unicode. When the program is run in python, an exception is thrown when a unicode character in one of the strings is first seen. The same program in ipython works, displaying all characters appropriately.</p>

<p>I've distilled the issue into a separate little program as follows:</p>

<pre><code>name = u'O\u2019Connor'
try:
   print name
except:
   print "exception 1 thrown"

try:
   print u"{}".format(name)
except:
   print "exception 2 thrown"

try:
   print u"%s" % name
except:
   print 'exception 3 thrown'
</code></pre>

<p>When run using python, exceptions are thrown everytime. When run in ipython, all three print statements work. Obviously, there is a difference between the two versions in the way unicode is handled. What is the difference and what should I do so that my program will handle being run in either environment?</p>

<p>Looks like ipython is using a sane default output encoding (probably UTF-8 or UTF-16), while plain Python is using <code>cp437</code>, a limited one-byte-per-character ASCII superset that can't represent the whole Unicode range.</p>

<p>If you can control the command prompt, you can run <code>chcp 65001</code> before launching Python to make it use the "code page" for UTF-8 (which Python should pick up on). You might want to make this <a href="http://superuser.com/q/269818/556135">the default for command prompts in general to avoid future problems</a>.</p>

python unicode ipython anaconda