Python Engine: How Python Executes Your Code
Python is one of the most interesting programming languages to learn, especially when you start wondering what actually happens behind the scenes after you click the Run button.
We usually write Python code, run it, and immediately see the output. But what happens between writing the code and getting the output?
In this article, I’ll explain the basic working of the Python execution engine in a simple and beginner-friendly way.
What Happens When We Run a Python Program?
Suppose we create a Python file called:
hello.pyInside the file, we write Python code such as:
print("Hello, World!")When we run this program, Python doesn’t simply take our code and directly give it to the computer’s hardware.
There are several steps involved.
A simplified execution flow looks like this:
Python Source Code → Compiler → Bytecode → Python Virtual Machine → Output
Let’s understand each part.
1. .py File
A .py file is a Python source file.
It contains the Python code written by the programmer.
For example:
name = "Nikesh"
print(name)The code inside this file is called source code because it is the code written by the programmer.
2. Running the Program
When we click the Run button in an IDE or execute the program using the terminal, the Python interpreter starts processing the Python source code.
For example:
python hello.pyAt this point, Python starts analyzing and processing the code.
3. Syntax Checking
Before Python can execute the program, it needs to make sure that the code follows Python’s syntax rules.
Syntax means the set of rules that define how Python code should be written.
For example:
print("Hello"This code is missing a closing parenthesis.
Python will detect this problem and produce a:
SyntaxErrorFor example:
SyntaxError: '(' was never closedIf the source code contains a syntax error, Python cannot continue with normal execution.
4. Compilation
This is where an important part of Python’s execution process happens.
Python source code is compiled into something called bytecode.
The bytecode is an intermediate representation of the Python program.
It is not the same thing as machine code.
For example, Python may internally generate bytecode instructions similar to:
LOAD_NAME
CALL
RETURN_VALUEThe exact bytecode depends on the Python version and the program being executed.
Python can also store compiled bytecode in .pyc files, commonly inside the __pycache__ directory.
So the simplified process is:
Source Code → Bytecode
5. Python Virtual Machine (PVM)
After the Python source code is compiled into bytecode, the bytecode is executed by the Python Virtual Machine (PVM).
The PVM is the part of the Python runtime that executes Python bytecode.
You can think of it as the execution environment that understands Python bytecode instructions and performs the required operations.
So the process becomes:
Python Source Code → Bytecode → PVM
6. Python Interpreter
The Python interpreter executes the bytecode instructions.
This is why Python is commonly described as an interpreted language.
However, saying that Python simply “executes source code line by line” is an oversimplification.
In CPython, which is the standard and most widely used Python implementation, the source code is first compiled into bytecode and then that bytecode is executed by the Python runtime.
So a better simplified model is:
Source Code → Compilation → Bytecode → Execution by the Python Virtual Machine
7. Machine Code and the Hardware
There is another important distinction.
Python bytecode is not the same as CPU machine code.
Machine code consists of instructions that a particular processor can execute directly.
Python normally uses the Python runtime to execute its bytecode rather than simply converting the entire Python program directly into CPU machine code.
The Python runtime itself is implemented in a lower-level language, primarily C in the case of CPython, and ultimately the operating system and CPU execute machine-level instructions.
Therefore, the simplified idea:
Get Nikesh Varma Kodali’s stories in your inbox
Join Medium for free to get updates from this writer.
Python Code → Binary → CPU
is not an accurate description of normal CPython execution.
A better representation is:
Python Source Code → Bytecode → Python Virtual Machine → System/Hardware
Understanding the Main Components
Now let’s summarize the important components involved in Python execution.
1. .py File
A .py file contains Python source code written by the programmer.
Example:
x = 10
print(x)2. Run
The Run button or terminal command starts the Python program.
For example:
python program.pyIt tells the Python runtime to execute the specified Python file.
3. Syntax Checking
Python checks whether the source code follows Python’s syntax rules.
For example:
if x > 10
print(x)This code produces a syntax error because the if statement is missing a colon.
Correct version:
if x > 10:
print(x)4. Compiler
The Python compiler converts Python source code into bytecode.
It does not simply convert Python directly into binary machine code.
The simplified process is:
Python Source Code → Bytecode
5. Python Virtual Machine (PVM)
The PVM executes the Python bytecode.
It is an important part of the Python runtime and is responsible for carrying out the operations represented by the bytecode.
6. Interpreter
The interpreter executes the bytecode through the Python runtime.
It should not be thought of as simply converting high-level Python code into low-level code line by line.
Instead, in CPython, the source code is compiled into bytecode, and the runtime executes that bytecode.
What About Errors?
During Python program execution, we mainly encounter two broad categories of problems:
1. Syntax Errors
A SyntaxError occurs when Python cannot parse the code because it doesn't follow Python's syntax rules.
Example:
print("Hello"Output:
SyntaxErrorThe program cannot proceed normally until the syntax problem is fixed.
2. Exceptions
Exceptions occur when something goes wrong while the program is running.
For example:
x = 10
y = 0print(x / y)This produces:
ZeroDivisionErrorAnother example:
number = int("hello")This produces:
ValueErrorExceptions are not limited to mathematical problems. They can occur for many different reasons, such as invalid input, missing files, incorrect data types, unavailable resources, and many others.
Final Execution Flow
We can summarize the Python execution process like this:
Python Source Code
↓
Syntax / Parsing
↓
Compilation
↓
Bytecode
↓
Python Virtual Machine
↓
Execution
↓
OutputThis is a simplified model of how Python programs are executed, particularly in CPython, the standard Python implementation.
Conclusion
When we write Python code and click the Run button, Python doesn’t simply send our code directly to the computer’s hardware.
The source code is first processed and checked for syntax problems. It is then compiled into bytecode, which is executed by the Python Virtual Machine as part of the Python runtime.
Understanding this process gives us a better idea of what is happening behind the scenes when we run even a simple Python program.
At first, Python execution may look like:
Write Code → Run → Output
But internally, there is much more happening between those steps.
And that is what makes understanding the Python execution engine so interesting.




