Python – Make Emacs use UTF-8 with Python Interactive Mode

emacsencodingpythonterminalutf-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'

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

Best Solution

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")