Skip to content

Module 1 · Introduction to Python — topic 12 of 14

Understanding the Python interpreter

What the interpreter does when you run Python: reading source code, compiling it to bytecode, executing it on the Python virtual machine, and reporting errors.

Intermediate 14 min read·Lesson 12 of 306 in Python Programming

Learning objectives

  • Describe the steps between source code and running program
  • Explain what bytecode and the Python virtual machine are
  • Inspect bytecode with the dis module

Definition

What the interpreter does when you run Python: reading source code, compiling it to bytecode, executing it on the Python virtual machine, and reporting errors.

Introduction

When you type python hello.py, a program called the interpreter does the work. Understanding a little of what happens inside it explains why some errors appear before anything runs and others only appear later.

The standard interpreter is called CPython because it is written in C. Other implementations exist, such as PyPy, but CPython is the one you installed.

Why it matters

Knowing how the interpreter works helps you read error messages correctly, understand why a typo stops the whole file from running, and later understand performance and the .pyc files you may notice in your project folders.

Detailed explanation

Step 1, parsing: the interpreter reads your source text and checks the grammar. A grammar mistake, such as a missing bracket, produces a SyntaxError and nothing in the file runs.

Step 2, compiling to bytecode: valid code is translated into bytecode, a compact list of simple instructions such as “load this value” or “add the top two values”. Bytecode is not machine code; it is designed for Python’s own virtual machine.

Step 3, executing: the Python virtual machine (PVM) runs the bytecode instructions one by one. Problems found while running, such as dividing by zero, are runtime errors.

Imported modules are cached as bytecode in __pycache__ folders (.pyc files) so they load faster next time. You can safely delete these folders; Python recreates them.

Syntax

Python
import dis
dis.dis(function_name)
  • dis is the standard-library disassembler.
  • dis.dis() prints the bytecode instructions of a function.
  • The exact instructions differ between Python versions.

Code examples

A function is compiled before it runs

Python
def double(n):
return n * 2
print(double.__code__.co_name)
print(double(21))
Output
double
42

__code__ is the compiled code object Python created from your source.

Viewing bytecode

Python
import dis
def double(n):
return n * 2
dis.dis(double)

This prints instructions such as LOAD_FAST n and RETURN_VALUE. The exact list depends on your Python version, so try it yourself.

More examples

Syntax errors stop everything, runtime errors stop at the line

Python
source_ok = "print('first')\nprint(1 / 0)"
source_bad = "print('first')\nprint('missing bracket'"
try:
compile(source_bad, "bad.py", "exec")
except SyntaxError:
print("bad.py: SyntaxError before anything ran")
try:
exec(compile(source_ok, "ok.py", "exec"))
except ZeroDivisionError:
print("ok.py: ran the first line, then failed at run time")
Output
bad.py: SyntaxError before anything ran
first
ok.py: ran the first line, then failed at run time

Step-by-step explanation

  • source_ok and source_bad hold two tiny programs as text.
  • compile() performs the parsing and bytecode steps; for source_bad it raises SyntaxError, so not even the first print runs.
  • source_ok compiles fine. exec() runs its bytecode: “first” is printed, then 1 / 0 fails while running.
  • The two except blocks show the difference between a compile-time and a run-time error.

Expected output

Running “Syntax errors stop everything, runtime errors stop at the line” prints exactly:

TextOutput
bad.py: SyntaxError before anything ran
first
ok.py: ran the first line, then failed at run time

Practical applications

  • Editors such as VS Code parse your file continuously to underline syntax errors before you run it.
  • Deployment tools pre-compile code to .pyc files to speed up start-up.
  • PyPy uses a just-in-time compiler to run some long programs much faster than CPython.

Best practices

  • Treat SyntaxError as a typing problem and look near the line shown (or just before it).
  • Ignore and gitignore __pycache__ folders.
  • Use dis only to learn; you never need to write bytecode.

Common mistakes

Believing Python ran half of a file with a syntax error

Avoid

Expecting earlier lines to print before a syntax error on line 20.

Prefer

Fix the syntax error first; nothing runs until the file parses.

Parsing happens before execution, so one grammar error stops the whole file.

Committing __pycache__ folders

Avoid

Adding __pycache__/ to version control.

Prefer

Add __pycache__/ to .gitignore.

They are generated automatically and differ between machines and versions.

Key takeaways

  • The interpreter parses, compiles to bytecode, then runs it on the Python virtual machine.
  • Syntax errors are found before running; runtime errors happen while running.
  • .pyc files in __pycache__ are cached bytecode.

Try it yourself

  1. 1
    Write a function that adds two numbers and view its bytecode with dis.dis on your computer.
  2. 2
    Create a file whose third line has a missing closing bracket and confirm that the first two lines don’t print.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

8 questions cover the “Introduction to Python” module. Answers and explanations are revealed after you submit.

  • BeginnerPython is best described as which kind of language?
  • BeginnerWho created Python?
  • BeginnerHow does Python group statements into blocks?
Practise this module