Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Is Python compiled or interpreted?

There are syntax errors that will prevent your Python code from running

x = 2
print(x)

if x > 3

File "examples/other/syntax_error.py", line 4
    if x > 3
           ^
SyntaxError: invalid syntax

There are other syntax-like errors that will be only caught during execution

x = 2
print(x)
print(y)
y = 13
print(42)

2
Traceback (most recent call last):
  File "compile.py", line 5, in <module>
    print y
NameError: name 'y' is not defined
def f():
    global y
    y = "hello y"
    print("in f")

x = 2
print(x)
f()
print(y)
y = 13
print(42)

2
in f
hello y
42
  • Python code is first compiled to bytecode and then interpreted.
  • CPython is both the compiler and the interpreter.
  • Jython and IronPython are mostly just compiler to JVM and CLR respectively.