If you read PEP 263 , it clearly states:
To determine the encoding of the source code, the magic comment must be placed in the source files either on the first or second line in the file ...
(The original sentence said that this should be the first line after # !, if there is one, but apparently it turned out to be easier to implement using the "first or second line" rule.)
Actual reference documents describe the same thing in a less friendly but more rigorous way, for 3.3 and 2.7 .
The "magic comment" that appears later in the file is not magical, it is just a comment that misleads you without affecting the Python compiler.
UTF-8 for u'εε' is '\xe5\x93\x88\xe5\x93\x88' , so these are bytes in the file. In recent versions of Python (including 2.7 and all 3.x), the default encoding is always ASCII if the file does not start with the UTF specification (as some Microsoft editors do); even in 2.3-2.6 it is usually ASCII; in earlier versions it is Latin-1. Attempting to interpret '\xe5\x93\x88\xe5\x93\x88' will fail with the exact exception that you saw.
source share