Showing posts with label environment. Show all posts
Showing posts with label environment. Show all posts

Python and UTF-8 on OSX: UnicodeEncodeError

an acute problem

UnicodeEncodeError:
'ascii' codec can't encode character u'\xe9'

I'm on a Mac Air with the latest Snow Leopard (10.6.2). I'm using Python 2.6.4 with unicode strings. I can't print appliqué !?

I tried adding
# -*- coding: utf-8 -*-
at the head of my Python script, but I still get this complaint.

Solution

The encoding used for standard input, output, and standard error can be specified by setting the PYTHONIOENCODING environment variable before running the interpreter.

The value should be a string in the form <encoding> or <encoding>:<errorhandler>. The encoding part specifies the encoding’s name, e.g. utf-8 or latin-1; the optional errorhandler part specifies what to do with characters that can’t be handled by the encoding, and should be one of “error”, “ignore”, or “replace”.

export PYTHONIOENCODING=utf-8

does the trick.

– or you could just add this setting to your environment file: ~/.MacOSX/environment.plist

Setting environment variables

There is a special environment file which loginwindow reads each time a user logs in. The environment file is: ~/.MacOSX/environment.plist

Dot files aren't, by default, visible in the finder, so type cd; open .MacOSX in a terminal. If the environment.plist file is there you can open it in the Property List editor with a doubleClick. Otherwise, you will have to create the .MacOSX directory yourself.

To create the directory and file
$ cd
$ mkdir .MacOSX
$ echo "" > .MacOSX/environment.plist
$ open .MacOSX/environment.plist

This should open your new, empty environment.plist file in the Property List Editor. Select Root and Add a Child: the Key should be the name of the variable you want to set; Type should be String; Value should be the value you want to set for this variable. Next time you log in the variables should be set.

Environment variables set in environment.plist also appear in the standard bash environment. You can see all your environment variables using the shell command env from a Terminal window.

REFERENCE: http://developer.apple.com/qa/qa2001/qa1067.html

WARNING: do not set the DISPLAY variable—see my post re. X11 on Leopard.