Engineering Articles · 10 min read

Python's Inner Working - Behind The Scenes

How Python executes code under the hood.

On this page
  1. Step 1: You Write Source Code
  2. Step 2: Tokenizing (Lexical Analysis)
  3. Step 3: Parsing and Building the AST
  4. Step 4: Compiling to Bytecode
  5. Step 5: The Python Virtual Machine (PVM)
  6. Step 6: Stack-Based Execution
  7. Step 7: Frames and the Call Stack
  8. Step 8: Memory Management and Reference Counting
  9. Step 9: The Garbage Collector
  10. Step 10: The Global Interpreter Lock (GIL)
  11. Putting It All Together

Most people write Python code, run it, and see the output. It feels simple. You type print("Hello"), hit run, and the words appear on screen. But a lot happens between typing that line and seeing the result. Let's walk through the entire journey, step by step, in plain and simple language. (If you've read how JavaScript executes code, the first few steps will feel familiar. The two languages start the same way and then take different paths.)

Step 1: You Write Source Code

Everything starts with a .py file. This file is just plain text. The computer does not understand plain text as instructions. It only understands machine code, which is a bunch of 1s and 0s specific to your CPU. So Python has to convert your readable code into something the machine can actually run.

Step 2: Tokenizing (Lexical Analysis)

The first real step is called tokenizing. Python reads your source code character by character and breaks it into small pieces called tokens.

For example, this line:

python
x = 5 + 3

gets broken into tokens like:

TokenKind
xA name
=An operator
5A number
+An operator
3A number

Think of tokens as the "words" of your code. Just like a sentence is made of words, your code is made of tokens. This step does not care about meaning yet. It just identifies the pieces. (Python's tokenizer also produces tokens you can't see, like NEWLINE, INDENT and DEDENT. That's how indentation becomes part of the grammar.)

Step 3: Parsing and Building the AST

Once Python has tokens, it needs to understand the structure and grammar of your code. This is where the parser comes in.

The parser takes the tokens and arranges them into a tree structure called an AST, which stands for Abstract Syntax Tree. This tree represents the grammatical structure of your code, similar to how a sentence has subjects, verbs, and objects.

For x = 5 + 3, the tree would roughly look like this in concept:

mermaid
flowchart TD
    A[Assign] --> T["Target: x"]
    A --> B["Value: BinOp"]
    B --> L["Left: 5"]
    B --> O["Op: +"]
    B --> R["Right: 3"]

This tree makes it easy for Python to understand what operation depends on what. If your code has a syntax error, like a missing colon or an unclosed bracket, this is usually the stage where Python catches it and throws a SyntaxError.

You can actually see this tree yourself using Python's built in ast module:

python
import ast
tree = ast.parse("x = 5 + 3")
print(ast.dump(tree, indent=2))

Step 4: Compiling to Bytecode

Now that Python has a clean tree structure, it compiles this tree into something called bytecode. Bytecode is a low level, simplified set of instructions. It is not the same as machine code, but it is much closer to something a machine can process quickly.

The compiler also does a little tidying on the way. For x = 5 + 3 it notices that both sides are constants and simply stores 8, so the addition never happens at runtime. This is called constant folding.

When you import a module, its bytecode is saved as a .pyc file inside a folder called __pycache__. This is why the second import of a module (in a later run of your program) can start a little faster. Python does not need to tokenize and parse again if the source file has not changed. It just reuses the saved bytecode. (The script you run directly, python app.py, is compiled fresh every time. Only imported modules get cached.)

You can view the bytecode of any function using the dis module:

python
import dis

def add(a, b):
    return a + b

dis.dis(add)

This will output something like:

text
LOAD_FAST                a
LOAD_FAST                b
BINARY_ADD
RETURN_VALUE

These instructions are simple stack based operations. Python is telling itself: load this value, load that value, add them, and return the result.

(The exact names depend on your Python version. From Python 3.11 onwards, BINARY_ADD is replaced by a general BINARY_OP instruction with + as its argument, and you'll also see a RESUME instruction at the start. The idea is identical.)

Step 5: The Python Virtual Machine (PVM)

Here is the important part. Your CPU does not directly execute bytecode. It is executed by a system called the Python Virtual Machine, often abbreviated as PVM. This is basically a big loop written in C (since standard Python, called CPython, is written in the C programming language) that reads bytecode instructions one by one and performs the matching action.

This loop is sometimes called the "eval loop" internally. Think of it like a translator standing between your bytecode and your actual hardware. It reads an instruction like BINARY_ADD, understands what it means, and then calls the correct C function to actually perform that addition using your CPU.

This is also the reason Python is considered an interpreted language. Unlike languages like C or Rust, which compile directly to machine code that your CPU runs on its own, Python always needs this virtual machine sitting in between, translating instructions on the fly.

Recent versions have started borrowing ideas from JavaScript engines:

  • Python 3.11 added a specializing adaptive interpreter. While your code runs, the eval loop notices patterns (say, a + that always sees two integers) and swaps the generic instruction for a faster specialized one, falling back if the types change. It's the same type-feedback idea V8 uses before its JIT kicks in, and it's a big part of why 3.11 was noticeably faster.
  • Python 3.13 added an experimental JIT compiler that can turn hot bytecode into machine code. It's off by default and still early, but it shows where CPython is heading. (Alternative implementations like PyPy have used a JIT for years.)

Step 6: Stack-Based Execution

Python's virtual machine uses something called a stack to keep track of values while executing instructions. A stack is a simple structure where the last thing you add is the first thing you take out, like a stack of plates.

Going back to our earlier example:

text
LOAD_FAST a       # push value of a onto the stack
LOAD_FAST b       # push value of b onto the stack
BINARY_ADD        # pop both values, add them, push result
RETURN_VALUE      # pop and return the result
text
After LOAD_FAST a:   [ a ]
After LOAD_FAST b:   [ a, b ]
After BINARY_ADD:    [ a+b ]
After RETURN_VALUE:  [ ]        → a+b is returned

Every single operation in Python, no matter how complex your code looks, eventually breaks down into these tiny stack operations.

Step 7: Frames and the Call Stack

Every time a function is called in Python, a new frame is created. A frame is like a small workspace that holds:

  • Local variables for that function
  • The bytecode being executed
  • A pointer to where execution currently is
  • A reference back to the calling frame

These frames stack on top of each other, forming what is called the call stack. If function A calls function B, and B calls function C, you get three frames stacked up. When C finishes, its frame is removed, and control goes back to B's frame, and so on.

text
C()   ← running now
B()   ← waiting for C
A()   ← waiting for B

This is also why deep recursion in Python can cause a RecursionError. Each recursive call adds a new frame, and there is a limit to how many frames can pile up: 1,000 by default, which you can check with sys.getrecursionlimit().

Step 8: Memory Management and Reference Counting

While all this execution is happening, Python is also managing memory behind the scenes. Every object in Python, whether it is a number, string, list, or custom class instance, has something called a reference count. This number tracks how many places in your program are currently pointing to that object.

python
a = [1, 2, 3]   # reference count of the list becomes 1
b = a           # reference count becomes 2
del a           # reference count becomes 1
del b           # reference count becomes 0, memory is freed

When the reference count drops to zero, Python knows nobody needs that object anymore, so it frees the memory immediately.

(You can peek at the count with sys.getrefcount(obj), but it will always show one more than you expect, because passing the object into the function temporarily adds a reference of its own.)

Step 9: The Garbage Collector

Reference counting alone has a weakness. It cannot handle situations where two objects reference each other, but nothing else references either of them. This is called a reference cycle.

python
class Node:
    def __init__(self):
        self.other = None

a = Node()
b = Node()
a.other = b
b.other = a

Even if you delete a and b from your code, they still reference each other, so their reference count never reaches zero on its own. To handle this, Python has a separate system called the garbage collector, which specifically looks for these cycles and cleans them up periodically.

mermaid
flowchart LR
    A["Node a"] -->|other| B["Node b"]
    B -->|other| A

The garbage collector is generational. New objects start in the youngest generation, which is checked often, because most objects die young. Objects that survive a few collections move to older generations that are checked less and less, so long-lived objects don't get rescanned all the time. You can trigger a collection yourself with gc.collect().

Step 10: The Global Interpreter Lock (GIL)

One more important piece is the GIL, short for Global Interpreter Lock. In standard Python (CPython), only one thread can execute Python bytecode at any given moment, even if your computer has many CPU cores.

This exists mainly to keep memory management, especially reference counting, safe and simple. Without the GIL, two threads could try to change a reference count at the same time and corrupt the data.

This is why Python threads are great for tasks that wait around a lot, like downloading files or reading from a database (a thread waiting on I/O releases the GIL so others can run), but not great for tasks that need heavy CPU computation at the same time. For true parallel CPU work, people often use multiple processes instead of multiple threads, using a module like multiprocessing. (More on threading models across languages in Concurrency and Parallelism.)

Two more things worth knowing:

  • Heavy libraries can escape it. NumPy and similar C extensions release the GIL while they crunch numbers, which is why they can use several cores even from threaded Python code.
  • The GIL is becoming optional. Python 3.13 introduced an experimental "free-threaded" build without a GIL, and 3.14 made it officially supported, though not yet the default. Threads in that build can truly run Python code in parallel.

Putting It All Together

Summary from start to finish:

mermaid
flowchart TD
    S["Source code (.py file)"] --> T[Tokenizer: tokens]
    T --> P["Parser: Abstract Syntax Tree"]
    P --> C[Compiler: bytecode]
    C -. imported modules .-> PC["__pycache__ (.pyc)"]
    C --> VM["Python Virtual Machine: eval loop"]
    VM --> ST[Stack-based execution inside frames]
    ST --> M["Reference counting + garbage collector free memory"]
  1. You write source code in a .py file
  2. Python tokenizes the code into small pieces called tokens
  3. The parser builds an Abstract Syntax Tree from those tokens
  4. The compiler turns that tree into bytecode
  5. The bytecode gets cached in __pycache__ for faster future runs (for imported modules)
  6. The Python Virtual Machine reads the bytecode instruction by instruction
  7. Each instruction runs using a stack-based system
  8. Function calls create frames that stack on top of each other
  9. Reference counting tracks and frees memory automatically
  10. The garbage collector cleans up any leftover reference cycles
  11. The GIL makes sure only one thread touches bytecode at a time

This process explains a lot of real behaviour you see every day as a developer:

  • Python is slower than compiled languages like C because of that extra virtual machine translation layer.
  • The first import of a large module can feel slow, but later ones are faster because of bytecode caching.
  • Deep recursion fails with an error because of the frame stack limit.
  • Threads do not speed up CPU-heavy tasks the way you might expect, because of the GIL.
  • Memory usually cleans itself up nicely, thanks to reference counting and the garbage collector working together.

Python hides all of this complexity behind a clean and simple-looking language. But underneath that simplicity is a well-designed pipeline, turning your readable code into something a machine can actually execute, step by careful step.