Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why UnicodeEncodeError raised only in emacs's python shell?

In emacs (use run-python command to call python3):

>>> sys.version
sys.version
'3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
>>> sys.getdefaultencoding()
sys.getdefaultencoding()
'utf-8'
>>> data
data
'sp\xe4m'
>>> print(data)
print(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character '\xe4' in position 2: ordinal not in range(128)

In terminal:

~$python3
Python 3.2.3 (v3.2.3:3d0686d90f55, Apr 10 2012, 11:25:50)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> data='sp\xe4m'
>>> sys.getdefaultencoding()
'utf-8'
>>> data
'späm'
>>> print(data)
späm

Does anyone have ideas about the reason that unicode string of Python works in terminal but not in emacs

(Version information of emacs : GNU Emacs 24.2.1 (x86_64-apple-darwin, NS apple-appkit-1038.36) of 2012-08-27 on bob.porkrind.org)

like image 843
Hanfei Sun Avatar asked Jul 18 '26 10:07

Hanfei Sun


1 Answers

I believe that this is related: https://emacs.stackexchange.com/a/9876/861

Adding

(setenv "LANG" "en_US.UTF-8")
(setenv "LC_ALL" "en_US.UTF-8")
(setenv "LC_CTYPE" "en_US.UTF-8")

to your .emacs solved the problem for me.

like image 171
Dror Avatar answered Jul 20 '26 22:07

Dror



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!