使Emacs使用UTF-8与Python交互模式 [英] Make Emacs use UTF-8 with Python Interactive Mode
问题描述
当我从Mac OS'Terminal.app启动Python时,python将该编码识别为UTF-8:
When I start Python from Mac OS' Terminal.app, python recognises the encoding as UTF-8:
$ python3.0
Python 3.0.1 (r301:69556, May 18 2009, 16:44:01)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'UTF-8'
这对python2是一样的。 5,
This works the same for python2.5.
但是在Emacs里面,编码是US-ASCII。
But inside Emacs, the encoding is US-ASCII.
Python 3.0.1 (r301:69556, May 18 2009, 16:44:01)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'US-ASCII'
如何使Emacs与Python,以便sys.stdout知道使用UTF-8?
How do I make Emacs communicate with Python so that sys.stdout knows to use UTF-8?
编辑:由于我没有代表编辑接受的答案,这里正是为我在Aquaemacs 1.6,Mac OS 10.5.6上工作。
Since I don't have the rep to edit the accepted answer, here is precisely what worked for me on Aquaemacs 1.6, Mac OS 10.5.6.
在python-mode-hook中,我添加了
In the python-mode-hook, I added the line
(setenv "LANG" "en_GB.UTF-8")
显然,Mac OS需要UTF-8,而dfa则表示Ubuntu需要UTF8。
Apparently, Mac OS requires "UTF-8", while dfa says that Ubuntu requires "UTF8".
另外,我不得不通过执行Cx RET p设置输入/输出编码,然后键入utf-8两次。我应该知道如何永久地设置这个。
Additionally, I had to set the input/output encoding by doing C-x RET p and then typing "utf-8" twice. I should probably find out how to set this permanently.
感谢dfa和Jouni共同帮助我找到答案。
Thanks to dfa and Jouni for collectively helping me find the answer.
这是我最后的python-mode-hook:
Here is my final python-mode-hook:
(add-hook 'python-mode-hook
(lambda ()
(set (make-variable-buffer-local 'beginning-of-defun-function)
'py-beginning-of-def-or-class)
(define-key py-mode-map "\C-c\C-z" 'py-shell)
(setq outline-regexp "def\\|class ")
(setenv "LANG" "en_GB.UTF-8"))) ; <-- *this* line is new
推荐答案
您的环境变量:
$ LANG="en_US.UTF8" python -c "import sys; print sys.stdout.encoding"
UTF-8
$ LANG="en_US" python -c "import sys; print sys.stdout.encoding"
ANSI_X3.4-1968
in your python hook, try:
< (setenvLANGen_US.UTF8)
这篇关于使Emacs使用UTF-8与Python交互模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!