【问题标题】:Make Emacs use UTF-8 with Python Interactive Mode让 Emacs 使用 UTF-8 和 Python 交互模式
【发布时间】:2010-10-27 16:01:18
【问题描述】:

当我从 Mac OS 的 Terminal.app 启动 Python 时,python 将编码识别为 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 也是一样的。

但在 Emacs 中,编码是 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?


编辑:由于我没有代表来编辑接受的答案,这正是在 Aquaemacs 1.6、Mac OS 10.5.6 上对我有用的方法。

在python-mode-hook中,我添加了这一行

(setenv "LANG" "en_GB.UTF-8")

显然,Mac OS 需要“UTF-8”,而 dfa 说 Ubuntu 需要“UTF8”。

此外,我必须通过执行 C-x RET p 然后键入“utf-8”两次来设置输入/输出编码。我可能应该找出如何永久设置它。

感谢 dfa 和 Jouni 共同帮助我找到答案。

这是我最后的 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

【问题讨论】:

  • 注意:在 ubuntu 上 LANG 必须设置为 en_GB.UTF8,没有 -
  • 谢谢,我更正了我的总结。
  • 在 ubuntu 上没有破折号?我总是使用 UTF-8 格式,它可以工作。

标签: python emacs encoding utf-8 terminal


【解决方案1】:

检查您的环境变量:

$ 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 钩子中,尝试:

(setenv "LANG" "en_US.UTF8")

【讨论】:

  • 那是python-mode-hook,对吧?设置 LANG 并没有改变任何东西。不过,(Aqua)emacs 确实没有获取我的环境变量。
  • 我认为它需要在 OS X 上是“en_US.UTF-8”——注意连字符。要让 Emacs 获取您的环境变量,您可以使用属性列表编辑器在 ~/.MacOSX/environment.plist 中全局设置它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多