When to use utf8 as a header in py files

A more direct answer:

In Python 3+: you don't need to declare any encoding.

UTF-8 is the default. Make sure the file is encoded in UTF-8 without BOM. Some Windows editors don't have it by default. It won't hurt to declare it, and some editors may use it.

In Python 2: always.

The default is OS dependent. As first or second line line of your files put this comment:

# -*- coding: utf-8 -*-

And remember: this is just about your source code files. Now in the 3rd millennium the string type does not exist anymore. You must take care of the type text, that is a sequence of bytes and an encoding. You'll still have to define your encoding in all input and output operation. These operations will still be dependent on your environment, so it's still better to follow the rule: Explicit is better than implicit.


wherever you need to use in your code chars that aren't from ascii, like:

ă 

interpreter will complain that he doesn't understand that char.

Usually this happens when you define constants.

Example: Add into x.py

print 'ă'

then start a python console

import x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "x.py", line 1
 SyntaxError: Non-ASCII character '\xc4' in file x.py on line 1, 
   but no encoding declared;
   see http://www.python.org/peps/pep-0263.html for details

Tags:

Python

Utf 8