7

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'

This works the same for python2.5.

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'

How do I make Emacs communicate with Python so that sys.stdout knows to use UTF-8?


Edit: 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.

In the python-mode-hook, I added the line

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

Apparently, Mac OS requires "UTF-8", while dfa says that Ubuntu requires "UTF8".

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.

Thanks to dfa and Jouni for collectively helping me find the answer.

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
smci
  • 32,567
  • 20
  • 113
  • 146
Nathan Shively-Sanders
  • 18,329
  • 4
  • 46
  • 56

1 Answers1

7

check your environment variables:

$ 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:

(setenv "LANG" "en_US.UTF8")
dfa
  • 114,442
  • 31
  • 189
  • 228
  • That's python-mode-hook, right? Setting LANG didn't change anything. It is true that my environment variables aren't being picked up by (Aqua)emacs, though. – Nathan Shively-Sanders May 20 '09 at 15:28
  • I think it needs to be "en_US.UTF-8" on OS X - note the hyphen. To make Emacs pick up your environment variables, you can set them globally in ~/.MacOSX/environment.plist using Property List Editor. – Jouni K. Seppänen May 20 '09 at 16:47