News & Updates

How to Use PEP 22 for Declaring File Encoding in Python

By Caitlin Rhodes 8 min read 3130 views

How to Use PEP 22 for Declaring File Encoding in Python

Ever opened a Python script that suddenly throws a SyntaxError because of a stray character? Most of the time it’s not the code itself but the way the file tells Python to interpret its bytes. That’s where PEP 22 steps in – it defines a simple, portable way to declare the character encoding of a source file.

What PEP 22 Actually Says

In a nutshell, PEP 22 says: place a comment on one of the first two lines of the file, beginning with # coding: followed by the encoding name. Python will read that line before anything else, know how to decode the rest of the source, and continue execution.

Typical examples look like this:

# -*- coding: utf-8 -*-
# coding=latin-1

The first form mimics the old Unix “magic comment” style, while the second is the minimal syntax. Both are accepted, and both work on any platform that supports the stated encoding.

Why You Might Need It

  • Non‑ASCII characters: If your comments, string literals, or identifiers contain characters like “é”, “ñ”, or “ß”, you must tell Python how to read them.
  • Cross‑platform scripts: A file created on Windows in cp1252 will break on Linux unless you declare the encoding explicitly.
  • Legacy codebases: Older projects often used latin-1 or iso-8859-1. Declaring the original encoding avoids subtle bugs after migration.

Getting the Syntax Right

The declaration has a few non‑obvious rules that catch people out:

Location Matters

It must appear on either the first or second line of the file. If you put a docstring before it, Python will already have tried (and failed) to decode the file.

Spacing Is Flexible

Both # coding: utf-8 and #coding=utf-8 are valid. Even extra spaces around the colon are tolerated, but stray characters after the encoding name will raise an error.

Supported Encodings

Python’s standard library knows a handful of encodings out of the box: utf-8, utf-16, utf-32, plus the classic Latin‑1 family. If you need something exotic, you’ll have to register a codec first – but that’s a rare edge case.

Practical Steps to Add an Encoding Declaration

Here’s a quick checklist you can follow whenever you start a new script or rescue an old one:

  • Open the file in a text editor that shows line numbers.
  • Make sure the first line is either a shebang (#!/usr/bin/env python3) or a blank line.
  • Insert the coding comment on the next line if you need it.
  • Save the file using the same encoding you declared.

For example, a typical UTF‑8 script might start like this:

#!/usr/bin/env python3

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

def greet(name):

print(f"Olá, {name}!")

greet("Mário")

Notice the shebang stays on line 1, the comment on line 2, and the rest of the code follows naturally.

Common Pitfalls and How to Avoid Them

Even after you add the comment, you can still hit snags. Below are the most frequent mistakes and quick fixes.

  • Mismatched file save encoding: Your editor might still be saving as cp1252 while you declared utf-8. Double‑check the editor’s encoding settings.
  • Wrong comment format: Using # coding utf-8 (missing colon or equals) will be ignored.
  • Non‑ASCII in the comment itself: The comment must be pure ASCII; otherwise Python can’t even parse the declaration.
  • Multiple declarations: Only the first valid declaration counts. Later lines are treated as regular comments.

When Not to Worry About Encoding

If your codebase is strictly ASCII – that is, it contains only characters in the 0‑127 range – you can safely omit the declaration. Python defaults to UTF‑8 in versions 3.0 and later, so even plain ASCII files are interpreted as UTF‑8 without any extra hint.

But be cautious with third‑party libraries that might read your source files as text; they sometimes assume a platform default encoding, which can differ from Python’s internal default.

Testing Your Declaration

The easiest way to verify that the encoding is being honoured is to run the file with the -X utf8 flag turned off (Python 3.7+). If the script still runs, you’re good to go. Alternatively, you can deliberately insert an invalid byte sequence and watch Python raise a UnicodeDecodeError at import time – that’s a strong sign the declaration is active.

Bottom Line

PEP 22 is a tiny piece of the Python ecosystem, yet it saves you from a lot of head‑scratching when dealing with international text. By placing a single comment on the first or second line, you tell the interpreter exactly how to read the rest of the file. Remember the key points – correct placement, proper syntax, and matching file save encoding – and you’ll rarely see encoding‑related errors again.

Python Enhancement Proposal (PEP) | Python | Veri Bilimi ve Veri Analizi
PEP 257: Python Docstring Standards | PDF | Class (Computer Programming ...
Python PEP 8 코드 스타일 가이드 적용 및 자동 포맷팅 | LabEx
GitHub - huyinit/PEP-8-Style-Guide-for-Python-Code: PEP-8-Style-Guide ...

Written by Caitlin Rhodes

Caitlin Rhodes is a Chief Correspondent with over a decade of experience covering breaking trends, in-depth analysis, and exclusive insights.