Common Header Format in Python

A header is a block of comments at the top of the code, which includes the filename, author, date, and a few other details of the file and the contents of that file. In-built modules imported and third parties imports follow this. This format may differ from programmer to programmer.

In this tutorial article, we will introduce the most common header format for Python source files.

The following code shows a common header format used in Python.

#!/usr/bin/env python3  Line 1
# -*- coding: utf-8 -*- Line 2
# Created By  : name_of_the_creator   Line 3
# Created Date: date/month/time ..etc
# version ='1.0'
# ---------------------------------------------------------------------------
""" Details about the module and for what purpose it was built for"""  #Line 4
# ---------------------------------------------------------------------------
# Imports Line 5
# ---------------------------------------------------------------------------
from ... import ...  #Line 6

The first line, (#!/usr/bin/env python3 ), makes it easier to run the file as a script by invoking the interpreter implicitly as this line is an executable script. It also informs the user that the code is written for a particular format, Python3 in this case.

The line ( -*- coding: utf-8 -*-) is used for encoding. It is the process of converting data from one form to another. UTF stands for Unicode Transformation Format, and 8 denotes that 8-bit values are being used in the encoding process. There are multiple types of encodings (UTF-16, UTF-34, etc.), but in python 3, the default character set is UTF-8.

The third line contains the details about the author, date, version number, credits, email, status, license, copyrights, etc.

The fourth line is a docstring denoted by (""" """), which documents the module and for what purpose it has been built. It is a convenient way to associate with all functions, classes, and methods. It is uniquely specified to describe a particular code.

The following line contains the imports of built-in modules used in the code, for example, os Module, sys Module, math Module, etc.

All the imports of third-party modules, for example, NumPy, Matplotlib, etc., are specified in the final line.

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.