Python
Code Comments
File Headers
Programming
Software Development

What is the common header format of Python files?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Python does not require a heavy file header, and most modern projects keep the top of each file minimal. The usual pattern is a shebang only when the file is meant to be executed directly, an encoding declaration only when needed, then a module docstring, followed by imports.

The usual top-of-file order

For a normal Python module, the common layout is:

  1. optional shebang
  2. optional encoding declaration
  3. module docstring
  4. imports
  5. constants, classes, and functions

Here is a typical executable script:

python
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4"""Command-line utility for generating daily reports."""
5
6from pathlib import Path
7import sys
8
9
10def main() -> int:
11    print("Running report job")
12    return 0
13
14
15if __name__ == "__main__":
16    raise SystemExit(main())

That layout is widely recognized and works well with tooling, editors, and other developers.

When to use a shebang

The shebang line matters only when the file is executed directly by the operating system:

python
#!/usr/bin/env python3

Use it for command-line scripts that may be run as:

bash
./myscript.py

If the file is just an imported module inside a package, a shebang is usually unnecessary.

When to use an encoding declaration

In modern Python 3, UTF-8 is the default source encoding, so an encoding header is often omitted. You still may see:

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

This line is mostly for clarity or compatibility with older tooling. It is not mandatory in every file.

If your project uses plain UTF-8 and modern editors, many teams choose to skip the encoding line entirely.

The module docstring is the real header

The most useful header content in Python is usually the module docstring. It explains what the file is for in one or two short paragraphs.

python
"""Utilities for validating user-provided CSV files before import."""

That string becomes the module documentation and is accessible through introspection:

python
1import importlib
2
3module = importlib.import_module("math")
4print(module.__doc__ is not None)

A concise docstring is usually more valuable than a large comment block full of author names, dates, or revision history that version control already tracks better.

Imports should come immediately after the docstring

Once the optional top lines and the module docstring are in place, imports normally come next.

python
1"""Helpers for working with local cache files."""
2
3from pathlib import Path
4import json

This matches the style expected by tools such as formatters, linters, and documentation generators.

What not to put in the header

Many languages historically used giant comment banners with metadata such as author, created date, change log, and copyright notices. Python projects often avoid that unless a legal or organizational rule requires it.

A header like this is usually unnecessary:

python
1# Author: ...
2# Date: ...
3# Last modified: ...
4# Version: ...

Why avoid it:

  • version control already tracks change history
  • author information becomes stale quickly
  • repeated boilerplate adds noise without helping readers

If a license notice is required, keep it short and consistent with the project’s legal conventions.

Practical examples

Minimal library module:

python
"""String normalization helpers."""

import re

Executable script:

python
1#!/usr/bin/env python3
2
3"""Sync files from a source directory into an archive directory."""
4
5from pathlib import Path

Module with explicit encoding for compatibility:

python
# -*- coding: utf-8 -*-
"""Parsers for multilingual input data."""

These are all valid and common. The "right" header is mostly about the role of the file, not about satisfying a rigid universal template.

Common Pitfalls

The biggest mistake is assuming every Python file needs a long metadata banner. In most codebases, that creates clutter and duplicates information that Git already stores more accurately.

Another issue is adding a shebang to library modules that are never executed directly. It is harmless, but it can mislead readers about the file’s purpose.

Developers also keep encoding declarations out of habit even when the project is standard UTF-8 on Python 3. That is not wrong, but it is often unnecessary.

Finally, do not skip the module docstring just because the file seems obvious today. A short description at the top often helps more than any other part of the header when someone returns to the code later.

Summary

  • Python file headers are usually minimal rather than elaborate.
  • Use a shebang only for executable scripts.
  • Use an encoding declaration only when needed or required by project conventions.
  • A concise module docstring is the most useful header element in most files.
  • Let version control track change history instead of copying that metadata into every file.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.