ProjekinAja. Software Engineering Stage 1 · Foundations Lesson 1.1
● Stage 1 · Lesson 1.1

From Source to Execution

What actually happens between typing python train.py and silicon doing work — and why "Python is slow" is a statement about this pipeline, not your code.

⏱ ~16 min read 🧠 Mental model 🚀 Course entry point

🎯 By the end you'll understand

The real pipeline from .py text to a running process — tokens, AST, bytecode, the interpreter loop — where the stack and heap actually live, what the OS contributes, why CPython pays a 10–100× tax on pure-Python loops and why that almost never explains your slow program. You'll also meet TraceLab v0, the deliberately scruffy script this whole course is built around.

KEY INSIGHTThe machine never runs your code

The CPU has never executed a line of Python in its life. It executes an interpreter — a program written in C — whose input happens to be your program.

You've typed python train.py thousands of times. Here's the uncomfortable question this lesson answers: what, physically, happens next? If your honest answer is "Python… runs it," you're carrying a magic-shaped hole in your mental model — and you cannot debug, profile, or reason about an abstraction you believe is magic. Every skimmed traceback and every repeated "Python is slow" lives in that hole.

🔑 Hold onto this

When you run python train.py, the operating system starts a process running a compiled C program called python. Your file is just data that program reads. It tokenizes the text, parses it into a tree, compiles the tree to bytecode, and then sits in a loop executing those bytecode instructions one by one. Your code is never "run" directly — it is interpreted by a program that is itself being run. Once you see this clearly, performance, tracebacks, imports, and the entire rest of this course stop being folklore.

HOW IT WORKSFrom text to bytecode: the part everyone skips

Take the smallest training script imaginable — four lines a robotics researcher might actually write:

train_step.py4 lines
# the whole program
lr = 0.1
epochs = 3
total = lr * epochs
print(total)   # → 0.30000000000000004  (…wait, what? Lesson 1.5)

To the interpreter, this file is a string of characters — no more meaningful than a CSV. Before anything "runs," CPython pushes it through three transformations, each one trading a flexible representation for a stricter one:

1

Tokenize — text becomes a word stream

The tokenizer (lexer) chops characters into tokens: NAME 'lr', OP '=', NUMBER '0.1'… Whitespace becomes structure here: your indentation is converted into real INDENT/DEDENT tokens. This stage checks spelling, not meaning — lr * = 3 tokenizes fine and dies later.

2

Parse — the stream becomes a tree

The parser (a PEG parser since Python 3.9) matches tokens against the language grammar and builds an abstract syntax tree. Precedence and nesting now live in the tree's shape: lr * epochs is a BinOp node with two Name children. A SyntaxError is this stage refusing your tokens — nothing has executed yet.

3

Compile — the tree becomes bytecode

Yes, compile. CPython has a real compiler; it walks the AST and emits bytecode — flat, numbered instructions for a virtual stack machine. For imported modules this output is cached on disk as .pyc files in __pycache__/, which is why second imports are faster. (Your top-level script is recompiled every run — only imports get the cache.)

4

Execute — a loop eats the opcodes

Only now does "interpretation" begin: the virtual machine fetches bytecode instructions one at a time and executes each one. Everything you call "running Python" is this loop.

train.py source text tokenizer tokens flat stream parser AST syntax tree compiler bytecode VM opcodes feeds CPython VM the ceval loop syscalls — write(), read(), mmap()… OS kernel → CPU
The CPython pipeline. Everything left of the VM happens once per run (and is cached on disk for imported modules). The VM loop on the right is where your program spends its life.

You can watch stage 3's output yourself with the standard library's dis module. Here is the disassembly of train_step.py, lightly trimmed (modern CPython adds bookkeeping opcodes like RESUME and PUSH_NULL, plus inline caches — noise for today):

python3 -m dis train_step.pytrimmed
# line 1
LOAD_CONST     0.1          # push the float object
STORE_NAME     lr           # pop it, bind the name 'lr'
# line 2
LOAD_CONST     3
STORE_NAME     epochs
# line 3
LOAD_NAME      lr           # push whatever 'lr' refers to
LOAD_NAME      epochs
BINARY_OP      *            # pop two, push the product
STORE_NAME     total
# line 4
LOAD_NAME      print
LOAD_NAME      total
CALL           1            # pop arg + callee, push the result
POP_TOP                     # discard print's None
RETURN_VALUE
📖 So Python is compiled?

Partly, yes — and this is the first crack in the "interpreted vs compiled" story. CPython compiles your source to bytecode, then interprets the bytecode. The thing it never does (by default) is translate your code to native machine instructions. Hold that thought for the comparison section.

CORE CONCEPTThe interpreter loop: a switch statement eating opcodes

The CPython virtual machine — the famous ceval loop, in Python/ceval.c — is conceptually tiny: an infinite loop wrapped around a giant switch. Fetch the next bytecode instruction, jump to its case, do the work, advance the instruction pointer, repeat. It is a stack machine: instructions don't name registers, they push and pop values on a value stack. BINARY_OP * means "pop two objects, multiply them, push the result." That's the whole execution model.

Now look at what one innocent lr * epochs costs inside that loop. A C compiler would emit roughly one machine instruction for a float multiply. CPython instead must: fetch and dispatch the opcode, look up two names, check the runtime types of both operands (float? int? a NumPy array with __rmul__?), unbox the actual numbers from their heap objects, multiply, allocate a brand-new float object for the result, and update reference counts along the way. Hundreds of machine instructions to perform one multiply — and that ratio, not anything about your code, is the literal content of the sentence "Python is slow."

💡 The interpreter is getting smarter, not different

CPython 3.11's adaptive specializing interpreter (PEP 659) watches the running bytecode and rewrites hot instructions into specialized variants — "I've only ever seen floats here, skip the full dispatch" — for a ~25% average speedup. Python 3.13 added an experimental JIT (PEP 744). The pipeline you just learned isn't obsolete trivia; it's exactly the machinery these optimizations operate on.

Two practical consequences fall out immediately. First, tracebacks are the VM's call stack, printed — every frame, including the library frames, is just more of the same list; learn to read below your own code. Second, every line costs several opcodes, so a pure-Python inner loop multiplies the fixed per-opcode overhead by its iteration count. That's why vectorizing with NumPy works: it doesn't make Python faster, it removes Python from the loop.

SEE ITStep the pipeline yourself

Reading about the pipeline is one thing; watching the instruction pointer and value stack mutate is what makes it stick. Pick a snippet, walk it through all four stages, then execute it one opcode per click. Pay attention to the loop snippet: count how many opcode executions four lines of Python turn into.

⚙ Interactive · Execution pipeline step-runner

Tokenize → parse → compile → execute, one opcode per click. Disassembly lightly simplified (RESUME, PUSH_NULL and inline caches omitted); opcode names follow modern CPython.

1

Tokenize

source text → token stream

2

Parse

tokens → abstract syntax tree

3

Compile

AST → bytecode for the stack machine

4

Execute

the ceval loop eats opcodes, one per click

Value stack
—
Names bound
—
Stdout
—
Opcodes executed
0

This is plain text. Nothing has "run" — there is nothing to run yet.

Three things worth noticing. The branch snippet shows the instruction pointer jumping — the else-arm's opcodes exist but never execute. The loop snippet turns 4 lines into 35 opcode executions, because the VM re-fetches and re-dispatches the same instructions every iteration — the per-opcode tax, multiplied. And in the first snippet, 0.1 * 3 prints 0.30000000000000004: your first sighting of IEEE 754, which gets a full autopsy in Lesson 1.5.

CORE CONCEPTThe machine beneath: process, stack, heap

Step down one layer. When you hit Enter on python train.py, your shell asks the kernel to start a new process (via fork and exec, which get their own treatment in Lesson 1.3). The OS hands that process a private virtual address space, loads the CPython binary's machine code into it, and points the CPU at the first instruction. From then on the CPU does the only thing it knows: fetch an instruction, decode it, execute it, billions of times per second. All of those instructions belong to the interpreter. None of them are "yours."

Inside the address space, two regions matter for the rest of your career:

one process — its virtual address space machine code the CPython binary (compiled C) heap every Python object lives here: ints, lists, dicts — even your frames C stack the interpreter's own calls the only thing the CPU ever executes — your .py file never gets this honor; it stays data id(obj) is just an address in here; Lesson 1.2 maps this region object by object surprise: Python call frames are NOT here — they're heap objects, which is how generators pause mid-call and survive a return
Inside the process the OS gives you. The CPU fetch–decode–executes the interpreter's machine code; every object your script creates is a heap allocation it must later garbage-collect.

Here's the detail that separates people who've looked from people who haven't: Python's "call stack" is not the machine stack. CPython allocates its frame objects on the heap. That sounds like trivia until you realize it's the mechanism behind generators and coroutines — a heap-allocated frame can be paused, kept alive, and resumed long after a C-stack frame would have been obliterated. It's also why Python function calls are expensive: each one allocates and initializes a real object.

And the OS? It owns everything your process thinks it's doing to the outside world. Your code cannot touch a file, the network, the screen, or the clock — it can only ask the kernel via syscalls. print(total) bottoms out in write(1, b"0.30000000000000004\n", 20): a request to the kernel to copy bytes to file descriptor 1. The interpreter is a tenant; the OS is the landlord. Lesson 1.3 is the lease.

COMPARISON"Interpreted vs compiled" is mostly marketing

You now know enough to retire one of programming's laziest dichotomies. "Python is an interpreted language" is sloppy twice over: interpreted describes an implementation, not a language (PyPy runs the same Python with a JIT; C interpreters exist), and CPython compiles before it interprets. The honest question is "what does the CPU end up executing, and when did the translation happen?"

RuntimeWhat the CPU actually executesWhen translation happensHonest label
CPythoninterpreter machine code, walking your bytecodesource → bytecode at import timecompiles, then interprets
PyPyJIT-emitted machine code for hot loopsat runtime, after profiling real behavioran interpreter that compiles you
Java (HotSpot)bytecode interpretation, then JIT'd machine codeAOT to .class, JIT at runtime"compiled," blurrily
JavaScript (V8)tiered: interpreter → optimizing JITparse at load, JIT at runtimethe "interpreted" label died in 2008
C / Rustyour program, directlyahead of time, on the developer's machinecompiled — though still linked to a runtime
NumPy kernelpre-compiled C/Fortran loopsahead of time, on a build serverPython as the glue around it

A JIT (just-in-time) compiler is the blur made explicit: it interprets first, watches which code is hot and which types actually flow through it, then compiles that to specialized machine code mid-run — optimizing against observed reality instead of static possibility, at the price of warm-up and memory. The boundary you were taught as a binary is a dial.

🧪 Test your model

Why does a heavy PyTorch training run spend <5% of its time "being Python"? Because the Python you wrote executes a few thousand opcodes to orchestrate — build the graph, launch the step — and then control passes into pre-compiled C++/CUDA kernels where the actual FLOPs happen. Your interpreter tax applies only to the orchestration layer. This is the standard, correct architecture — not a workaround.

IN PRACTICESlow-but-productive: the actual tradeoff

So how slow is the interpreter, really? For pure-Python numeric inner loops, the commonly measured range is 10–100× slower than C — workload-dependent, but the right order of magnitude. You watched the reasons accumulate in the widget: per-opcode dispatch, dynamic type checks on every operation, every value a heap-allocated object (a Python int costs 28 bytes — check sys.getsizeof(1)), reference-count traffic on every push and pop.

Here's the part the "Python is slow" crowd skips: every one of those costs is a feature being paid for. Dynamic dispatch is why you can hand a NumPy array to code written for floats. Boxed objects are why everything is introspectable at a REPL. No compile step is why your experiment loop is seconds, not minutes. Guido van Rossum pitched Python as a "glue" language back in the 1990s, and that's still the honest job description: Python orchestrates; compiled kernels compute. NumPy, PyTorch, and JAX are the 10–100× tax being routed around, deliberately, by people who understood this lesson.

⚠ When Python genuinely is the wrong tool

Honesty cuts both ways. A 1 kHz robot control loop with hard real-time deadlines should not run through the ceval loop — not mainly because of average speed, but because of variance: garbage collection and allocation make latency unpredictable. This is why production robotics stacks put planning in Python and the control loop in C/C++ — a division of labor, not a verdict on either language.

The rule that will serve you all course: constants vs complexity. The interpreter costs a constant factor — 10×, maybe 100×. A wrong algorithm costs a growing one. When your script is slow, the interpreter is the suspect everyone arrests; the algorithm is usually the culprit. Lesson 1.6 turns that into a budget you can compute.

TRACELABTraceLab v0 is born

Meet the other protagonist of this course. Your experiment results live in terminal scrollback, notebook cells, and a doomed spreadsheet. Tools like W&B exist, but you want it self-hosted and you want to understand it. So today, TraceLab begins life the way most real infrastructure does: as a single scruffy script someone wrote in twenty minutes.

tracelab.pyv0 — born scruffy, on purpose
# tracelab.py — v0: log experiment runs to a CSV. It works. (It has debts.)
import sys, time

def log_run(values):
    f = open("runs.csv", "a")              # no encoding, no lock
    row = time.strftime("%Y-%m-%d %H:%M")
    for v in values:
        row = row + "," + v                # hope nobody passes a comma
    f.write(row + "\n")
    f.close()

def find_run(name):
    for line in open("runs.csv"):          # full scan, every single call
        if name in line:                   # substring match. what could go wrong
            return line.strip()

if __name__ == "__main__":
    if sys.argv[1] == "log":
        log_run(sys.argv[2:])
    else:
        print(find_run(sys.argv[2]))

Usage: python tracelab.py log exp042 loss=0.31 reward=12.7, then python tracelab.py find exp042. You'll write and formally spec this yourself in Lesson 1.9; today it's your dissection specimen. Apply this lesson to one line — f.write(row + "\n") — and trace it all the way down: tokenizer emits NAME OP NAME ...; parser builds an Expr → Call node; compiler emits LOAD_NAME f, LOAD_ATTR write, BINARY_OP +, CALL 1; the VM executes them, calling into the io module's C code, which buffers the bytes; at close() the buffer flushes via a write() syscall; the kernel copies it to the page cache and, eventually, the disk. No magic at any layer. That's the Stage 1 milestone, and you just did it.

Now the honest part. This script is wrong in ways that are invisible today and expensive later — and we are keeping every one of them, because repaying these debts personally is the syllabus:

Debt in v0The day it bitesRepaid in
open() with no encoding; values glued with +a robot named Café-3 turns to mojibake; a comma in a value silently corrupts the rowLesson 1.5
metrics stored as strings; float round-tripsaggregated losses drift; two "equal" runs aren'tLesson 1.5
find_run re-scans the whole file; name in line substring-matchesexp4 happily returns exp42; lookups go O(n²) across a workflowLessons 1.6–1.7
no history — edits and deletes are forever"which version of the script logged these?"Stage 2 (git)
no locking — concurrent writers interleavetwo robots log at once; rows shear mid-lineStage 3
the CSV "schema" lives in nobody's headthe great migrationStage 4

WEAKNESSWhere this mental model bites

✗ Blaming the interpreter for your algorithm

The classic failure: a slow script, a confident "Python is slow," a week rewriting in C++ — for a 30× win, when replacing a list scan with a dict lookup was a 1,000× win available in five minutes. The interpreter tax is a constant; complexity grows. Profile before you blame the pipeline (python -m cProfile is right there), and remember that TraceLab v0's real problem is find_run, not CPython.

✗ Treating the lower traceback frames as noise

Engineers who think execution is magic read only the last line of a traceback — their line — and paste the rest into a chat window. But every frame is the same machinery you stepped through today, and "it crashed inside pandas" almost always means "I handed pandas garbage three frames up." Read down the stack. It's not someone else's code; it's your program's actual execution history.

✗ Cargo-cult performance moves

Deleting __pycache__ "to speed things up" (it's a cache; you just made imports slower), reaching for Cython before measuring, or benchmarking one time.time() call on a warm laptop. Mechanism without measurement is superstition with extra steps — the measurement discipline arrives with the cost models in Lesson 1.6.

KEY INSIGHTHow this connects

  • Memory (1.2) — we zoom into the heap you just located: references, aliasing, refcounts, and why b = a copies nothing.
  • The OS contract (1.3) — the process, syscalls, and signals we waved at today become the whole lesson.
  • Data representation (1.5) — 0.30000000000000004 gets its autopsy, and TraceLab's CSV format gets specified (wrongly, on purpose).
  • TraceLab v0 (1.9) — you write the script yourself, against a real requirements brief, and catalog its sins formally.

Everything in this course stands on today's move: replacing "it just runs" with a pipeline you can point at. When tests, containers, databases, and async arrive in later stages, they will all be variations of the same question — what is actually executing, and on whose behalf?

GLOSSARYKey terms introduced

Token
The smallest meaningful unit the tokenizer cuts source text into: names, operators, literals, INDENT/DEDENT.
AST (abstract syntax tree)
The tree the parser builds from tokens; program structure encoded as shape rather than text.
Bytecode
Flat, numbered instructions for Python's virtual stack machine; the compiler's output, cached in .pyc files.
Virtual machine (VM)
The interpreter loop (ceval) that fetches and executes bytecode instructions one at a time.
Value stack
The VM's working area: opcodes push and pop object references here instead of naming registers.
Heap
The memory region for objects with arbitrary lifetimes; every Python object — including call frames — lives here.
Process
The OS's unit of a running program: a virtual address space, scheduled time on the CPU, and a table of open resources.
JIT compiler
A runtime compiler that watches hot code paths and translates them to specialized machine code mid-execution.

ACTIVE RECALLCheck your understanding

Answer in your own words first, then reveal.

1When you run python train.py, what program is the CPU actually executing?›

The CPython interpreter — machine code compiled from C, loaded into a fresh process by the OS. Your .py file is just data that program reads, tokenizes, parses, and compiles to bytecode. The CPU never executes your Python text; it executes the interpreter interpreting it.

2"Python is interpreted, C is compiled." What's wrong with that sentence?›

Two things. Interpreted/compiled describes an implementation, not a language — PyPy JIT-compiles the same Python to machine code. And CPython itself compiles source to bytecode before interpreting it. The honest questions: what does the CPU end up executing, and when did translation happen? Every modern runtime sits somewhere on that dial, and JITs blur it completely.

3Why is a pure-Python loop ~10–100× slower than the same loop in C — and why does that rarely explain a slow training job?›

Each iteration re-runs the interpreter loop: opcode fetch and dispatch, name lookups, dynamic type checks, boxed heap objects, refcount updates — hundreds of machine instructions per conceptual operation. But that's a constant factor, and real training jobs spend their FLOPs inside pre-compiled C/CUDA kernels anyway. Slow programs are usually a growing factor — a wrong algorithm, like TraceLab v0's full-file scan — not the interpreter tax.

4What does the OS provide your running script that the interpreter doesn't?›

The process itself: a private virtual address space, scheduled CPU time, file descriptors, and the syscall interface. The interpreter can't touch files, network, or screen — it can only ask the kernel. print() bottoms out in a write() syscall to file descriptor 1. The interpreter is a tenant; the OS is the landlord.

Next: why b = a copies nothing — the machine beneath the variables →