171

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> -*-

=> encoding instead of coding.

So what is the correct way of declaring the file encoding?

Is encoding permitted because the regex used is lazy? Or is it just another form of declaring the file encoding?

I'm asking this question because the PEP does not talk about encoding, it just talks about coding.

Oli
  • 13,730
  • 8
  • 28
  • 35
  • 5
    By the way, for more flexibility and portability it is recommended to use `#!/usr/bin/env python` instead of `#!/usr/bin/python` – glarrain Apr 22 '14 at 20:44
  • 7
    I love the way none of the answers on this page have a simple, working example for say UTF8. StackOverly at its finest. – aaa90210 Mar 01 '16 at 03:47
  • 3
    I just wanted to add that Python 3 has changed the default encoding from `ascii` to `UTF-8`. Compare: [python 2.7 docs](https://docs.python.org/2.7/tutorial/interpreter.html#source-code-encoding) with [python 3.7 docs](https://docs.python.org/3.7/tutorial/interpreter.html#source-code-encoding). This means you can safely omit this encoding if you wanted to specify `UTF-8`. – gertvdijk Feb 12 '19 at 12:19

6 Answers6

165

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).

ivan_pozdeev
  • 28,628
  • 13
  • 85
  • 130
Rafał Dowgird
  • 38,640
  • 11
  • 73
  • 89
  • 11
    The `-*-` ensures that the line is recognised by GNU Emacs (a text editor popular with some programmers). Note that, contrary to this answer, both the Emacs form and the Vim form are 100% python-docs-recommendation-compatible (as they both match the regexp - "match", by long-standing convention, means "match anywhere in the string", contrary to Python's API). – martinjs Dec 07 '15 at 10:49
  • 1
    The specific Emacs requirements to embedded directives are documented at https://www.gnu.org/software/emacs/manual/html_node/emacs/Specify-Coding.html . In brief, the format for the start of the file is: `-*- var: value[; ...] -*-`. – ivan_pozdeev Feb 23 '16 at 12:08
40

Just copy paste below statement on the top of your program.It will solve character encoding problems

#!/usr/bin/env python
# -*- coding: utf-8 -*-
Harun ERGUL
  • 4,996
  • 5
  • 47
  • 55
40

PEP 263:

the first or second line must match the regular expression "coding[:=]\s*([-\w.]+)"

So, "encoding: UTF-8" matches.

PEP provides some examples:

#!/usr/bin/python
# vim: set fileencoding=<encoding name> :

 

# This Python file uses the following encoding: utf-8
import os, sys
Brad Solomon
  • 29,156
  • 20
  • 104
  • 175
vartec
  • 118,560
  • 34
  • 206
  • 238
3

As of today — June 2018


PEP 263 itself mentions the regex it follows:

To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file, such as:

# coding=<encoding name>

or (using formats recognized by popular editors):

#!/usr/bin/python
# -*- coding: <encoding name> -*-

or:

#!/usr/bin/python
# vim: set fileencoding=<encoding name> : 

More precisely, the first or second line must match the following regular expression:

^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)

So, as already summed up by other answers, it'll match coding with any prefix, but if you'd like to be as PEP-compliant as it gets (even though, as far as I can tell, using encoding instead of coding does not violate PEP 263 in any way) — stick with 'plain' coding, with no prefixes.

1

If I'm not mistaken, the original proposal for source file encodings was to use a regular expression for the first couple of lines, which would allow both.

I think the regex was something along the lines of coding: followed by something.

I found this: http://www.python.org/dev/peps/pep-0263/ Which is the original proposal, but I can't seem to find the final spec stating exactly what they did.

I've certainly used encoding: to great effect, so obviously that works.

Try changing to something completely different, like duhcoding: ... to see if that works just as well.

Lasse V. Karlsen
  • 350,178
  • 94
  • 582
  • 779
0

I suspect it is similar to Ruby - either method is okay.

This is largely because different text editors use different methods (ie, these two) of marking encoding.

With Ruby, as long as the first, or second if there is a shebang line contains a string that matches:

coding: encoding-name

and ignoring any whitespace and other fluff on those lines. (It can often be a = instead of :, too).

Matthew Schinckel
  • 32,344
  • 6
  • 71
  • 109