In short
When you press Run, Python doesn't read your code like a book in one go. It first translates the whole file into bytecode, then executes it line by line. Knowing this explains why some mistakes stop everything and others only appear halfway through.
CPython, the standard interpreter, first compiles your .py file into bytecode and then runs that bytecode line by line in a virtual machine. This is why Python finds some errors only when the line actually runs.

Think of it like this
The translator and the actor
A play written in English is going to be performed in Hindi. First a translator converts the whole script into simple stage cues. If a page is unreadable, the show is cancelled before it starts. Then the actor performs the cues one by one, and only discovers a missing prop when that scene arrives. Python's compiler is the translator, and its virtual machine is the actor.
Watch it happen
From hello.py to output
1/5You run python3 hello.py. CPython, the standard interpreter, reads the text of your file.
The big ideas
What you need to know
Syntax is checked first
Missing colons, unclosed quotes and bad indentation are caught while compiling, so nothing in the file runs at all.
Other errors wait for their line
Using an undefined name or dividing by zero only fails when that exact line executes. A bug inside an if that never runs can hide for weeks.
Bytecode is not machine code
It is a portable instruction set for the Python VM, which is why the same .py file runs on Windows, macOS and Linux.
__pycache__ is a speed-up
Compiled bytecode for imported modules is cached there so the next run starts faster. It is safe to delete and should be ignored in Git.
See it in code
Walk through the code
1print("Step 1: starting")2print("Step 2: still fine")3 4if False:5 print(discount) # never runs, so no error yet6 7print(discount) # NameError happens here, at run time8print("Step 3: never reached")1/4These run normally and their output appears, because Python executes line by line.
Steps 1 and 2 print, then the program stops with NameError: name 'discount' is not defined.
Save yourself the pain
Common mistakes
Oops
Assuming a script is bug-free because it ran once without errors.
Do this instead
Only the lines that actually ran were checked. Test the other branches too, like empty input or a different menu choice.
Oops
Committing __pycache__ folders and .pyc files to Git.
Do this instead
Add __pycache__/ to .gitignore. They are generated automatically and differ between Python versions.
Oops
Thinking "interpreted" means Python never compiles anything.
Do this instead
CPython compiles to bytecode first, then interprets that bytecode. It is a two-step process hidden behind one command.
Your mission
Catch the two kinds of error
Quick check
Line 10 of a script has a missing closing bracket. What happens to the print() on line 1?
Go deeper
- 1Look inside a __pycache__ folder after importing a module and spot the .pyc bytecode files.
- 2Use the dis module on a tiny function to see its bytecode instructions.
- 3Write a script with an error on the last line and notice that the earlier lines still run.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.