Skip to content
Kuza Ndoto Academy

How Python Runs Your Code

Beginner → IntermediateChapter 1 · Page 4 of 564 min readUpdated 15 Nov 2026

When you run a Python program, a program called the interpreter reads your code and carries it out. Knowing what happens behind the scenes helps you understand error messages, use the interactive shell confidently and explain why Python feels so quick to work with.

From your code to a result

Computers only understand very simple machine instructions, so something has to translate your readable Python into actions. The standard interpreter, called CPython (because it is written in the C language), does this in two stages:

  1. Compile to bytecode. Python first checks the whole file for grammar mistakes and translates it into bytecode: a compact, lower-level set of instructions. This is fast and happens automatically.
  2. Run the bytecode. The Python Virtual Machine (PVM), part of the interpreter, executes those instructions one by one, from top to bottom.
CPython interpreter Your code hello.py Compile to bytecode Python Virtual Machine runs it Output Hello, world! checks syntax first runs top to bottom
Figure 1. Typing python hello.py starts both stages inside the interpreter: the whole file is compiled to bytecode first, then the virtual machine runs it and you see the output.

You never have to start these stages yourself. Typing python file.py does both. When one of your files is imported by another, Python may save its bytecode in a __pycache__ folder (files ending in .pyc) so the next run starts faster. It's safe to ignore or delete that folder.

Code runs from top to bottom

Once a program starts, Python runs one statement at a time. If a line causes an error at run time, Python stops right there. Lines before it have already run; lines after it never run.

print("Line 1 runs")
print("Line 2 runs")
print(10 / 0)
print("Line 4 never runs")
Line 1 runs Line 2 runs Traceback (most recent call last): ... ZeroDivisionError: division by zero

The error report is called a traceback (the middle lines, shortened here, show the file name and line number). Dividing by zero is impossible, so Python raises a ZeroDivisionError on line 3 and the program ends.

Syntax errors stop everything

Grammar mistakes are different. Because Python checks the whole file during the compile stage, a SyntaxError anywhere means nothing runs at all, not even line 1.

print("Line 1")
print("Line 2"
print("Line 3")

This file prints nothing. Python reports a SyntaxError because the bracket opened on line 2 is never closed. Fix the bracket and all three lines run.

Two ways to run Python

1. Scripts

A script is a .py file you save and run. This is how you build real programs: the code is kept, can be edited and can be run as many times as you like. Most of this series uses scripts.

2. The interactive shell (REPL)

Type python (Windows) or python3 (macOS/Linux) in a terminal with no file name. Python prints a short banner with its version and then shows the >>> prompt. This is the interactive shell, also called the REPL: it Reads a line, Evaluates it, Prints the result and Loops back for more.

>>> 2 + 3
5
>>> name = "Baraka"
>>> name
'Baraka'
>>> print(name)
Baraka
>>> exit()

Notice two things. First, the shell shows the value of an expression straight away, without print(). Second, typing just name shows the string with quotes (the way a programmer would write it), while print(name) shows it without quotes (the way a user would read it).

To leave the shell, type exit(), or press Ctrl+Z then Enter on Windows, or Ctrl+D on macOS and Linux.

Common mistake

In a script, a line such as 2 + 3 on its own calculates 5 and then throws the result away, so nothing appears. Only the interactive shell echoes values automatically. In scripts, always use print() to see a result.

Script (.py file)Interactive shell
Best forReal programs you keepQuick experiments and checking ideas
Saved?YesNo, lost when you exit
Shows values automatically?No, use print()Yes
How to startpython file.pypython with no file

Interpreted vs. compiled languages

Languages such as C and C++ are usually compiled ahead of time into a machine-code program that runs directly on the processor. Python is usually called interpreted because the interpreter runs your code for you each time, after its own quick compile-to-bytecode step.

  • Advantages: fast to write and test, the same .py file runs on Windows, macOS and Linux, and you get helpful error messages.
  • Trade-off: pure Python code usually runs more slowly than compiled C. In practice, heavy number crunching is handed to libraries such as NumPy, which are written in fast compiled code underneath.
Note

CPython is the interpreter you download from python.org and the one this series assumes. Other interpreters exist, such as PyPy, but they run the same Python language.

Key points

  • The interpreter compiles your code to bytecode, then the Python Virtual Machine runs it top to bottom.
  • A run-time error stops the program at that line; a syntax error stops the whole file from running.
  • Scripts are saved .py files; the >>> shell is for quick experiments.
  • The shell echoes values automatically, but scripts need print().
Up next · Getting StartedComments & IndentationWhy whitespace matters in Python and how to document code.
→

Tip: use the ← and → keys to move between pages.