How Python Runs Your Code
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:
- 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.
- Run the bytecode. The Python Virtual Machine (PVM), part of the interpreter, executes those instructions one by one, from top to bottom.
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")
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.
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 for | Real programs you keep | Quick experiments and checking ideas |
| Saved? | Yes | No, lost when you exit |
| Shows values automatically? | No, use print() | Yes |
| How to start | python file.py | python 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
.pyfile 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.
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
.pyfiles; the>>>shell is for quick experiments. - The shell echoes values automatically, but scripts need
print().
Tip: use the ← and → keys to move between pages.
