I am trying python-sympy on Emacs using the the python shell. Problem arise when I try to display unicode characters. This is what I get:

>>> from sympy import *
>>> x = symbols('x')
>>> init_printing(use_unicode=True)
>>> solve(x**2 -2, x)
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/Library/Python/2.7/site-packages/sympy-0.7.7.dev-py2.7.egg/sympy/interactive/printing.py", line 30, in _displayhook
  UnicodeEncodeError: 'ascii' codec can't encode character u'\u221a' in position 2: ordinal not in range(128)

Is there a way of displaying unicode (I did not tried ein-package)?

  • 1
    Are you sure this is Emacs-specific and cannot be reproduced outside Emacs?
    – wasamasa
    Jul 22, 2015 at 12:32
  • Yes. Tried on Terminal it correctly displays unicode [-√2, √2] Jul 22, 2015 at 13:39
  • Could this be related to the locale of the shell running in Emacs? What if you try: import locale; locale.setlocale(locale.LC_ALL,'en_US.UTF-8') before you print out the Unicode characters?
    – wvxvw
    Jul 22, 2015 at 20:37
  • I got the same error. from sympy import *;import locale;locale.setlocale(locale.LC_ALL,'en_US.UTF-8') ;init_printing(use_unicode=True);solve(x**2 -2,x) Jul 23, 2015 at 7:57
  • Interesting. I can't get this to work in emacs either, or iTerm2, but it DOES work in OSX Terminal.
    – feoh
    Jul 23, 2015 at 16:24

2 Answers 2


I found the answer in this StackExchange answer. Basically, you need to be sure that the PYTHONIOENCODING environment variable is set to utf-8

To try this out in your running emacs you can do M-x setenv and answer the prompts accordingly.

So, this isn't actually an emacs specific problem at all. I saw the same problem and the same solution worked in iTerm2.


In order to get this working in Emacs.app in addition to setting PYTHIONIOENCODING, I also had to set the LANG variable, I now have the following lines in my .emacs:

 (setenv "PYTHONIOENCODING" "utf-8")
 (setenv "LANG" "en_US.UTF-8")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.