Correct way to define Python source code encoding
encoding, python
Solution
Check the docs here:
"If a comment in the first or second line of the Python script matches the regular expression `coding[=:]\s*([-\w.]+)`, this comment is processed as an encoding declaration"
"The recommended forms of this expression are
# -*- coding: <encoding-name> -*-
which is recognized also by GNU Emacs, and
# vim:fileencoding=<encoding-name>
which is recognized by Bram Moolenaar’s VIM."
So, you can put pretty much anything before the "coding" part, but stick to "coding" (with no prefix) if you want to be 100% python-docs-recommendation-compatible.
More specifically, you need to use whatever is recognized by Python and the specific editing software you use (if it needs/accepts anything at all). E.g. the `coding` form is recognized (out of the box) by GNU Emacs but not Vim (yes, without a universal agreement, it's essentially a turf war).
Problem
PEP 263 defines how to declare Python source code encoding. Normally, the first 2 lines of a Python file should start with: ``` #!/usr/bin/python # -*- coding: <encoding name> -*- ``` But I have seen a lot of files starting with: ``` #!/usr/bin/python # -*- encoding: <encoding name> -*- ``` I.e., it says `encoding` rather than `coding`. How should the file encoding be declared? Please use "SyntaxError: Non-ASCII character ..." or "SyntaxError: Non-UTF-8 code starting with ..." trying to use non-ASCII text in a Python script to close duplicate questions about syntax errors resulting from a missing or faulty encoding declaration. This question, on the other hand, is the canonical for questions about how the declaration is written and whether it is necessary.