MediumIs Python Compiled or Interpreted? | by Manas Jha | Oct, 2026 | Medium
Sitemap
Manas Jha

Manas Jha

Is Python Compiled or Interpreted?

2 min readOct 12, 2026

--

Python executes the code in two distinct phases:
First it compiles the source code into an intermediate format called bytecode.

Then the Python Virtual Machine (PVM) interprets that bytecode by executing it instruction by instruction.

Phase 1: Compilation to Bytecode

When a .py script is run, Python doesn't evaluate the text directly. It parses the text into an Abstract Syntax Tree (AST) and compiles it into bytecode (a set of platform-independent instructions). This is why you sometimes see a __pycache__ folder containing .pyc files.

Press enter or click to view image in full size

To inspect the exact bytecode that Python generates, using the built-in dis (disassembler) module.

import dis

def calculate_total(price, tax_rate):
tax = price * tax_rate
return price + tax

# Disassemble the function to see the bytecode
dis.dis(calculate_total)

Output:

2           0 LOAD_FAST                0 (price)
2 LOAD_FAST 1 (tax_rate)
4 BINARY_MULTIPLY
6 STORE_FAST 2 (tax)

3 8 LOAD_FAST 0 (price)
10 LOAD_FAST 2 (tax)
12 BINARY_ADD
14 RETURN_VALUE
  • Column 1: Line number in the source code.
  • Column 2 & 3: The bytecode instruction (Opcode) and its index (e.g., LOAD_FAST, BINARY_MULTIPLY).
  • Column 4: The argument and variable name the instruction interacts with.

Phase 2: Interpretation (Line-by-Line Execution)

Once the bytecode is generated, the PVM takes over.

Press enter or click to view image in full size

The PVM (Python Virtual Machine) is a stack-based machine; it executes instructions by pushing values onto an evaluation stack, performing operations on them, and popping the results off.

Get Manas Jha’s stories in your inbox

Join Medium for free to get updates from this writer.

To watch Python execute your code line-by-line in real-time, use built-in tools like the trace module or pdb (Python Debugger).

Using trace to log every executed line: Run your script from the terminal with the trace flag. It will print every line of source code to the console exactly as the PVM processes it.

python -m trace --trace my_script.py

Using pdb to step through execution manually: Insert the debugger into your script. When Python hits this line, execution pauses, allowing you to inspect variables and step forward manually.

import pdb

def calculate_total(price, tax_rate):
pdb.set_trace() # Execution pauses here
tax = price * tax_rate
return price + tax

calculate_total(100, 0.05)

In the pdb terminal, you type n (next) to execute the current line and move to the next, or p tax to print the value of a specific variable at that exact moment in time.

--

--