Sunday, March 14, 2010

How to define python source code encoding?

While using non-ASCII characters in your code, you might get this error:
SyntaxError: Non-ASCII character '\xc3' in file myprog.py on line 101, but no encoding declared;

Solution is to define encoding at the beginning of your source code.

Syntax: # -*- coding: desired_encoding -*-
Example: # -*- coding:Utf-8 -*-

For details check http://www.python.org/dev/peps/pep-0263/.