Skip to content

Commit 23dcccb

Browse files
committed
Issue python#29381: Clarify ordering of UNIX shebang line as source encoding line
1 parent 77bb0f4 commit 23dcccb

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

Doc/tutorial/interpreter.rst

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,24 @@ should follow. To display all these characters properly, your editor must
138138
recognize that the file is UTF-8, and it must use a font that supports all the
139139
characters in the file.
140140

141-
It is also possible to specify a different encoding for source files. In order
142-
to do this, put one more special comment line right after the ``#!`` line to
143-
define the source file encoding::
141+
To declare an encoding other than the default one, a special comment line
142+
should be added as the *first* line of the file. The syntax is as follows::
144143

145144
# -*- coding: encoding -*-
146145

147-
With that declaration, everything in the source file will be treated as having
148-
the encoding *encoding* instead of UTF-8. The list of possible encodings can be
149-
found in the Python Library Reference, in the section on :mod:`codecs`.
146+
where *encoding* is one of the valid :mod:`codecs` supported by Python.
150147

151-
For example, if your editor of choice does not support UTF-8 encoded files and
152-
insists on using some other encoding, say Windows-1252, you can write::
148+
For example, to declare that Windows-1252 encoding is to be used, the first
149+
line of your source code file should be::
153150

154151
# -*- coding: cp-1252 -*-
155152

156-
and still use all characters in the Windows-1252 character set in the source
157-
files. The special encoding comment must be in the *first or second* line
158-
within the file.
153+
One exception to the *first line* rule is when the source code starts with a
154+
:ref:`UNIX "shebang" line <tut-scripts>`. In this case, the encoding
155+
declaration should be added as the second line of the file. For example::
159156

157+
#!/usr/bin/env python3
158+
# -*- coding: cp-1252 -*-
160159

161160
.. rubric:: Footnotes
162161

0 commit comments

Comments
 (0)