Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared

Don't really know why Python couldn't handle non-ASCII characters in the source code well by default. Encounter a syntax error in while trying to print out some non-ASCII characters ( Chinese ) with Python.

 print " 我爱你"

> python

The interpreter throws out this error message :

File "", line 1 SyntaxError: Non-ASCII character '\xe6' in file on line 1, but no encoding declared; see for details

To fix, this problem. Include this line at the beginning of the your Python source code.

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

After modification :

 # - *- coding: utf- 8 - *-
 print " 我爱你"

and now > python should print out :


Hope this helps!

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.