What Is an Interpreter in Python? (How Python Runs Code)

So what is an interpreter in Python? It’s the program that reads your Python code and runs it. When you type python script.py, the interpreter (usually CPython) compiles the code to bytecode and executes it right away, with no separate build step:

python script.py      # the interpreter runs a file
python                # or start it interactively

The screenshots show Python 3.12.5 on Windows, including an interactive session I typed in by hand.

We’ll look at the interactive interpreter, check which interpreter you’re running, peek at the bytecode it executes, find the cached .pyc files, and compare Python’s interpreters.

The interactive Python interpreter (REPL)

Run python with no file name and you get the >>> prompt. Each line runs as soon as you press Enter, and expressions print their value automatically:

Command Prompt showing an interactive Python interpreter session calculating prices and checking the version, then exiting
Type a line, see the result instantly. exit() or Ctrl+Z then Enter leaves the interpreter on Windows.
>>> price = 19.99
>>> price * 3
59.97
>>> f"${price * 1.08:.2f}"
'$21.59'
>>> import sys
>>> sys.version_info[:2]
(3, 12)
>>> exit()

This read-eval-print loop is perfect for quick math, testing a function, or exploring a library. The -q flag just hides the version banner.

For running whole files, see how to run a Python script.

How does the Python interpreter run your code?

  1. Parse: it reads your source and checks the syntax. A SyntaxError stops everything here.
  2. Compile: it turns the code into bytecode, a compact set of low-level instructions.
  3. Execute: the Python virtual machine runs that bytecode one instruction at a time.

So Python is compiled, just not to machine code ahead of time. That’s why it’s called an interpreted language, and why you can run code without a build step.

Which Python interpreter am I using?

The sys and platform modules tell you exactly which interpreter is running your code:

import sys
import platform

print("implementation:", sys.implementation.name)
print("version:       ", platform.python_version())
print("compiler:      ", platform.python_compiler())
print("interpreter:   ", sys.executable.split("\\")[-1])
print("bytecode tag:  ", sys.implementation.cache_tag)

Output:

implementation: cpython
version:        3.12.5
compiler:       MSC v.1940 64 bit (AMD64)
interpreter:    python.exe
bytecode tag:   cpython-312
Command Prompt output of Python sys.implementation and platform showing the CPython interpreter, version and bytecode cache tag
CPython is the standard interpreter from python.org. The cache tag shows up in .pyc file names.

If you have several Pythons installed, sys.executable tells you which one ran the script. See installing multiple Python versions for keeping them straight.

Seeing the bytecode with the dis module

The dis module shows the bytecode the interpreter executes for a function. You’ll see instructions like loading variables, multiplying and calling round():

import dis

def total_with_tax(price, qty, rate=0.0825):
    return round(price * qty * (1 + rate), 2)

print(total_with_tax(19.99, 3))
dis.dis(total_with_tax)                 # the bytecode the interpreter actually runs

Output:

64.92
  3           0 RESUME                   0

  4           2 LOAD_GLOBAL              1 (NULL + round)
             12 LOAD_FAST                0 (price)
             14 LOAD_FAST                1 (qty)
             16 BINARY_OP                5 (*)
             20 LOAD_CONST               1 (1)
             22 LOAD_FAST                2 (rate)
             24 BINARY_OP                0 (+)
             28 BINARY_OP                5 (*)
             32 LOAD_CONST               2 (2)
             34 CALL                     2
             42 RETURN_VALUE
Command Prompt output of the Python dis module disassembling a function into bytecode instructions
Each line is one bytecode instruction for the CPython virtual machine.

You never need to write bytecode, but reading it explains things like why local variables are faster than globals.

What are .pyc files and __pycache__?

When you import a module, CPython saves its bytecode in a __pycache__ folder so the next import can skip compiling. The file name includes the interpreter version:

import pricing_mod                       # importing a module compiles it to a cached .pyc
import os

print(pricing_mod.add_tax(50.00))
print(os.listdir("__pycache__"))

Output:

54.12
['pricing_mod.cpython-312.pyc']
Command Prompt output showing a pricing module imported and its compiled pricing_mod.cpython-312.pyc file in __pycache__
cpython-312 in the file name ties the cache to this interpreter version.

These files are safe to delete; Python recreates them. Add __pycache__/ to your .gitignore. The script you run directly isn’t cached, only modules it imports.

compile() and exec(): the interpreter’s steps by hand

You can call the same two steps yourself. compile() turns text into a code object, and exec() runs it:

source = "subtotal = 4 * 12.50\nprint(f'subtotal: ${subtotal:.2f}')"

code_obj = compile(source, "<order>", "exec")   # source text -> code object (bytecode)
print(type(code_obj).__name__, "| names used:", code_obj.co_names)
exec(code_obj)                                  # the virtual machine runs it

Output:

code | names used: ('subtotal', 'print')
subtotal: $50.00

Never exec() text from users or the internet; it runs anything. It’s shown here only to make the interpreter’s pipeline visible.

Python interpreters compared

InterpreterWritten inNotes
CPythonCThe standard one from python.org; what most people mean by “Python”
PyPyRPythonHas a JIT compiler, often much faster for long-running pure-Python code
MicroPythonCA small Python for microcontrollers
Jython / IronPythonJava / C#Run on the JVM and .NET; behind on Python 3 versions

Not sure which Python language features changed between versions? Python 3 vs Python 2 covers the big break.

Good next reads for understanding how Python runs:

Frequently asked questions

What is an interpreter in Python?

It’s the program that reads Python source code and runs it. The standard one, CPython, compiles code to bytecode and executes it on a virtual machine.

Is Python compiled or interpreted?

Both, in a sense. CPython compiles to bytecode automatically, then interprets that bytecode. There’s no separate compile step for you.

How do I open the Python interpreter?

Type python (or py on Windows, python3 on macOS and Linux) in a terminal to get the >>> prompt.

How do I exit the Python interpreter?

Type exit(), or press Ctrl+Z then Enter on Windows or Ctrl+D on macOS and Linux.

Can I delete __pycache__ folders?

Yes. They only hold cached bytecode and Python rebuilds them when needed.

Where can I learn more about the interpreter?

Start with Using the Python Interpreter in the official tutorial.