使 Emacs 在 Python 交互模式下使用 UTF-8 [英] Make Emacs use UTF-8 with Python Interactive Mode

查看:25
本文介绍了使 Emacs 在 Python 交互模式下使用 UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从 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".

此外,我必须通过执行 C-x 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-cC-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

在你的 python 钩子中,尝试:

in your python hook, try:

(setenv "LANG" "en_US.UTF8")

这篇关于使 Emacs 在 Python 交互模式下使用 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆