Is Python compiled or interpreted?
Python is often called interpreted, but it is compiled to bytecode first, and a virtual machine then runs the bytecode. The standard implementation, CPython, has no JIT by default, so it never turns your code into machine code.
How it runs your code
CPython compiles each source file to bytecode when you run or import it, and it saves the result in .pyc files. A virtual machine then runs the bytecode. Python's own documentation calls that virtual machine “the interpreter”, and the name causes most of the confusion. A real interpreter executes the source text or the syntax tree directly and repeats the translation work as it goes. Shell scripts, early BASIC, Ruby 1.8 and Tcl before version 8.0 worked that way. CPython does not.
Does that make Python slow?
CPython 3.11 and later adapt the bytecode as the program runs, which speeds up code that runs often. CPython 3.14 has an experimental JIT that is off by default. We turned it on, and our program did not get faster. PyPy, a different Python implementation, has a full JIT. It ran our program about five times faster than CPython.
What we measured
One small program, an order with items, a coupon, a discount and tax, written as an average programmer would write it. Times are nanoseconds per call.
| Setting | Class code | Plain functions |
|---|---|---|
| CPython 3.14, no JIT | 1,531 | 708 |
| CPython 3.14, experimental JIT on | 1,570 | 662 |
| PyPy 7.3.20, JIT | 270 | 135 |
PyPy implements Python 3.11, so it runs a different language version from CPython 3.14. The PyPy class-code times ranged from 202 to 467 ns.
Questions
Is Python an interpreted language?
It is often called one, but the label fits an implementation better than a language. CPython compiles source to bytecode and a virtual machine runs it, and PyPy compiles hot code to machine code. Python's documentation calls the virtual machine the interpreter.
Is Python slow because it is interpreted?
Not for that reason. CPython compiles to bytecode and runs it on a virtual machine, and it has no JIT by default. PyPy, which has a JIT, ran our program about five times faster.
What is a .pyc file?
CPython saves compiled bytecode in .pyc files inside __pycache__ folders, so it can skip the compile step the next time you import the module.
Does Python ever produce machine code?
Not by default. Nuitka compiles Python to C, Cython translates Python-like code to C, and PyPy compiles hot code to machine code while the program runs.
Related
Is JavaScript compiled? · Is Ruby compiled? · Is Lua compiled? · Compiled vs interpreted · Why optimization decides speed