Here's a little program:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
print('abcd kΩ ☠ °C √Hz µF ü ☃ ♥')
print(u'abcd kΩ ☠ °C √Hz µF ü ☃ ♥')
On Ubuntu, Gnome terminal, IPython does what I would expect:
In [6]: run Unicodetest.py
abcd kΩ ☠ °C √Hz µF ü ☃ ♥
abcd kΩ ☠ °C √Hz µF ü ☃ ♥
I get the same output if I enter the commands on trypython.org.
codepad.org, on the other hand, produces an error for the second command:
abcd kΩ ☠ °C √Hz µF ü ☃ ♥
Traceback (most recent call last):
Line 6, in <module>
print(u'abcd kΩ ☠ °C √Hz µF ü ☃ ♥')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u03a9' in position 6: ordinal not in range(128)
Contrariwise, IDLE on Windows mangles the output of the first command, but doesn't complain about the second:
>>>
abcd kΩ ☠°C √Hz µF ü ☃ ♥
abcd kΩ ☠ °C √Hz µF ü ☃ ♥
IPython in a Windows command prompt or through Python(x,y)'s Console2 version both mangle the first output and complain about the second:
In [9]: run Unicodetest.py
abcd kΩ ☠ °C √Hz µF ü ☃ ♥
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (15, 0))
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
Desktop\Unicodetest.py in <module>()
4 print('abcd kΩ ☠ °C √Hz µF ü ☃ ♥')
5
----> 6 print(u'abcd kΩ ☠ °C √Hz µF ü ☃ ♥')
7
8
C:\Python27\lib\encodings\cp437.pyc in encode(self, input, errors)
10
11 def encode(self,input,errors='strict'):
---> 12 return codecs.charmap_encode(input,errors,encoding_map)
13
14 def decode(self,input,errors='strict'):
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2620' in position 8: character maps to <undefined>
WARNING: Failure executing file: <Unicodetest.py>
IPython inside Python(x,y)'s Spyder does the same, but differently:
In [8]: run Unicodetest.py
abcd kΩ ☠°C √Hz µF ü ☃ ♥
------------------------------------------------------------
Traceback (most recent call last):
File "Unicodetest.py", line 6, in <module>
print(u'abcd kΩ ☠°C √Hz µF ü ☃ ♥')
File "C:\Python26\lib\encodings\cp1252.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u03a9' in position 6: character maps to <undefined>
WARNING: Failure executing file: <Unicodetest.py>
(In sitecustomize.py, Spyder sets its own SPYDER_ENCODING
based on the locale module's encoding, which is cp1252
for Windows 7.)
What gives? Is one of my commands wrong? Why does one work on some platforms while the other works on other platforms? How do I print Unicode characters consistently without crashing or screwing up?
Is there an alternate terminal for Windows that behaves like the one in Ubuntu? It seems that TCC-LE, Console2, Git Bash, PyCmd, etc. are all just wrappers for cmd.exe rather than replacements. Is there a way to run IPython inside the interface that IDLE uses?