Python Script Structure and Execution

How Python Script is Excecuted

• The interpreter executes scripts “synchronously”
◇ Line by line; From top to bottom
• If a particular statement takes a while to compute or is waiting on a response from some external source (like making an API call and waiting on the response), Python stops and waits for the current statement to finish executing before moving on to the next one


Script vs. Module

• A Python file can be referred as a script or module
Script: the file is intended to be executed
Module: the file contents are meant to be imported and used by another calling script


Components of a Script / Module

• The linear execution order of statements within a Python file drives the following ordering convention:

1. The “Shebang” Line

• This line tells the shell attempting to run the script what interpreter should be used to "execute" the script
• The Python interpreter sees this line as a comment and ignores it

#!/usr/bin/env python

2. The Module Docstring

• Refers to the triple-quoted string at the beginning of the file

"""Module docstring."""

3. Import Statements

• Import other code into your script so that you can use their functionality

# Imports
import os
import sys

4. Module “Constants”

• Using all-CAPS variable names to indicate these variables shouldn't be changed
• Nothing in Python makes these “constant”

# Module Constants
START_MESSAGE = "CLI Inspection Script"

5. Module-Level “Global” Variables

• Every function and class within the module will have at least "read access" to these variables as they exist at the top-level "global" scope within a module

# Module "Global" Variables
location = os.path.abspath(__file__)

6. Module Functions and Classes

• A function (or class) definition is loaded (stored) in memory by the interpreter
• You must call a function (supplying any needed arguments) to execute it

# Module Functions and Classes
def main(*args):
    """My main script function.

    Displays the full patch to this script, and a list of the arguments passed to the script.

    print("Script Location:", location)
    print("Arguments Passed:", args)

7. The

if __name__ == '__main__'


• When a Python script is executed, it is given the internal __name__ of __main__ by the Python interpreter
• This allows us to use the __name__ variable to determine (at run time) if our Python file is being executed or imported

# Check to see if this file is the "__main__" script being executed
if __name__ == '__main__':
    _, *script_args = sys.argv

• You can call your "main" function whatever you like. There is no special significance of the function name main()



• There are 2 syntaxes for importing functionality to a script

# Import
## Syntax #1
import os

## Syntax #2
from os.path import abspath

• The from ___ import ___ syntax provides a way for us to pull in only the functionality we need, and simplify the names of deeply nested resources

# Making function call
## Using Syntax #1

## Using Syntax #2