SyntaxError: Non-ASCII character – Python with UTF-8 encoding

Question

:
My Python program produce a following error message upon execution:

 SyntaxError: Non-ASCII character '\xc4' in file test.py on line 1, but no encoding declared; 

Answer:

Normally the above error message is displayed by python when other characters other then ASCII are used withing your code. The solution is to either remove all non-ASCII characters or include the bellow line into your code to enable UTF-8 encoding:

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

This will allow you to print also non-ASCII character within your code example:

$ cat test.py
# - *- coding: utf- 8 - *-
print "Ľuboš"
$ python test.py
Ľuboš


Comments and Discussions
Linux Forum