Python Header (Shebang & Comments)

How to Use Shebang Lines and Header Comments in Python Scripts

What is a Shebang?

The shebang (#!) is a special character sequence at the very top of a script file that tells Unix-like operating systems which interpreter to use to run the script.

Why Use a Shebang?

Common Shebang Lines

#!/usr/bin/env python3
# Preferred for portability; finds python3 in your PATH

#!/usr/bin/python3
# Hardcoded path to python3 interpreter

Example Script Using Shebang

#!/usr/bin/env python3

def greet():
    print("Hello, World!")

if __name__ == "__main__":
    greet()

Making the Script Executable

After adding the shebang, make your script executable with the command:

chmod +x your_script.py

Then run it directly:

./your_script.py

Header Comments in Python Scripts

Besides the shebang, Python scripts often start with a header comment block. This is a multiline comment at the top of the file used to:

Example Header Comment Block

#(**/*+-(**/*+-(**/*+-(**/*+-(**/*+-(**/*+-(**/*+-    % PYTHON %    (**/*+-(**/*+-(**/*+-(**/*+-(**/*+-(**/*+-
#
#
#
# Python Training - Chapter - 3 - Exercise 4 - Question -1 
#
# Description:
#  The code takes two integers and does arithmetic operations on the integers.
# 
#
# Usage:
#  python addition.py 
#
#
#(**/*+-(**/*+-(**/*+-(**/*+-(**/*+-(**/*+-(**/*+-    % PYTHON %    (**/*+-(**/*+-(**/*+-(**/*+-(**/*+-(**/*+-

Why Use Header Comments?

Important Notes