DEV Community

drani Godfrey
drani Godfrey

Posted on

What Happens When You Run Python Code?

Python is a popular programming language, but have you ever wondered what happens behind the scenes when you run a Python program on your computer? In this article, we’ll explore the journey of Python code from a simple script on your hard drive to the output displayed on your screen.

A Simple Example

Let’s start with a simple Python program. Imagine we have a file called greet.py stored on our computer. Inside this file, there’s a single function called greet():

#greet.py 

def greet(name:str):
    print(f"Hello {name}!")
Enter fullscreen mode Exit fullscreen mode

If we call this function with the argument "drani":

greet(name="drani")
Enter fullscreen mode Exit fullscreen mode

the program prints:

Hello drani!
Enter fullscreen mode Exit fullscreen mode

This is straightforward, but what actually happens when you execute this program?
Running the Python Program

To run the program, we typically use the command:

python greet.py

When this command is executed, several things happen behind the scenes.

1. Loading the Code

First, the Python interpreter (CPython) loads the code from disk into memory. This is the first step in preparing the program for execution.

2. Compilation to Bytecode

Next, the interpreter invokes its internal compiler. The process includes:

Tokenization: Breaking the code into meaningful symbols called tokens.

Building an Abstract Syntax Tree (AST): Organizing the tokens into a tree structure that represents the code’s logic.

Compiling to bytecode: Transforming the AST into Python bytecode, a lower-level representation of your code that the interpreter can execute efficiently.

If a cached version of the bytecode already exists in a .pyc file inside the pycache directory, Python may load that instead of recompiling the source file. This helps speed up execution for frequently used modules.

3. Executing Bytecode

Once the bytecode is ready, the CPython Virtual Machine executes it instruction by instruction. For each operation (opcode):

The interpreter dispatches the opcode to a corresponding C function that implements the operation.
These C functions are compiled into native machine code, binary instructions (zeros and ones) that the CPU can understand.
The CPU fetches, decodes, and executes these instructions, manipulating Python objects in memory as needed.

This process continues until all instructions have been executed.

4. Producing the Output

Finally, when execution completes, the resulting values are stored in memory. If the program includes output commands (like print()), the results are displayed to the user on the screen.

Conclusion

Even a simple program like greet.py involves several layers of processing under the hood. From loading the file and compiling it to bytecode, to executing machine-level instructions, Python hides a lot of complexity from the programmer making it easy to write and run code while still being efficient and powerful.

Understanding this process gives a deeper appreciation of how Python works and can help you write more efficient programs, troubleshoot performance issues, and better understand errors when they occur.

Top comments (0)