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
3
  • note: on ubuntu LANG must be set to en_GB.UTF8, without - Commented May 20, 2009 at 19:25
  • Thanks, I corrected my summary. Commented May 21, 2009 at 3:44
  • without dash on ubuntu? I always use the form UTF-8 and it works. Commented Feb 5, 2010 at 9:33

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.