IT Training
Blog
Search

Why Python Is an Interpreted Language (Bytecode and the PVM Explained)

Monitor showing program code in a dark editor theme

Python is called an interpreted language because you run your source code directly with the python command, and the interpreter executes it step by step at run time, without a separate compile step that produces a machine-code program. Under the hood, the standard interpreter CPython first compiles your code to bytecode and then a virtual machine runs that bytecode. Knowing this two-step process is what interviewers really want to hear when they ask why Python is an interpreted language.

Key takeaways

  • CPython, the standard Python, compiles .py source code to bytecode and then the Python Virtual Machine interprets that bytecode line by line.
  • There is no separate build step that produces a native executable, which is why Python is classed as interpreted.
  • Bytecode is cached in __pycache__ folders as .pyc files so imported modules start faster next time.
  • Being interpreted makes Python easy to test and portable, but usually slower than compiled languages for heavy number crunching.
  • Interpreted or compiled is a property of an implementation, not the language: PyPy, for example, uses a just-in-time compiler.

Compiled versus interpreted languages

A compiled language such as C or C++ is translated by a compiler into machine code for a specific processor before it runs. You get an executable file, and running it does not need the source code or the compiler.

An interpreted language is run by another program, the interpreter, which reads the code and carries out each instruction while the program runs. You ship the source code, and the user needs the interpreter installed.

PointCompiled (for example C)Interpreted (for example Python)
TranslationWhole program before runningWhile the program runs
OutputNative executableNo executable; source plus interpreter
Error discoveryMany errors at compile timeMany errors only when that line runs
SpeedUsually fasterUsually slower
PortabilityRebuild for each platformSame code runs wherever the interpreter runs

Why Python is an interpreted language: what actually happens

When you type python app.py, CPython does three things:

  1. Parse: it reads your source code and checks the syntax. A missing colon is caught here, before any line runs.
  2. Compile to bytecode: it converts the code into bytecode, a set of simple, platform-independent instructions.
  3. Interpret: the Python Virtual Machine (PVM) executes the bytecode one instruction at a time.

The compile step is automatic and invisible, and its output is bytecode for the PVM, not machine code for your processor. Because the final execution is done by an interpreter loop, Python is described as interpreted. Some people call it compiled and interpreted, and that answer is also accurate.

See the bytecode yourself

The built-in dis module shows the bytecode for any function:

import dis

def add(a, b):
    return a + b

dis.dis(add)

You will see instructions such as LOAD_FAST and RETURN_VALUE. The exact names vary between Python versions, which is another reason bytecode is an internal detail and not something you ship.

What are .pyc files?

When you import a module, CPython saves its bytecode in a __pycache__ folder as a .pyc file. Next time, if the source has not changed, Python loads the cached bytecode and skips the compile step. The main script you run directly is not cached this way. Deleting __pycache__ is always safe; Python recreates it.

A simple way to see interpretation in action

print('Step 1 runs')
print('Step 2 runs')
print(undefined_name)
print('Step 4 never runs')

The first two lines print, then you get a NameError. A compiler for a statically typed language would refuse to build the program at all. Python only discovers the problem when it reaches that line, because names are looked up at run time. This is why tests matter so much in Python projects.

Advantages and disadvantages of Python being interpreted

Advantages

  • Fast feedback: edit and run immediately, or test ideas in the interactive shell or a Jupyter notebook.
  • Portability: the same script runs on Windows, macOS and Linux if the interpreter is installed.
  • Dynamic features: dynamic typing and run-time introspection make code short and flexible.
  • Easier debugging: errors point to the exact line that failed, with a readable traceback.

Disadvantages

  • Speed: pure Python loops are slower than the same code in C or Java.
  • Late errors: type mistakes may only appear when a rarely used path runs.
  • Distribution: users need Python installed, or you must bundle it with a tool such as PyInstaller.

If Python is slow, why is it used for data science and AI?

Because the heavy work does not run in the Python interpreter. Libraries such as NumPy, pandas and PyTorch do their number crunching in optimised C, C++ or GPU code. Python acts as the easy, readable layer that calls them. You get Python's simplicity for writing the logic and compiled speed for the maths.

Other Python implementations

  • CPython: the reference implementation from python.org, written in C. This is what most people use.
  • PyPy: uses a just-in-time (JIT) compiler that turns hot code into machine code while running, often making long-running pure Python programs much faster.
  • Jython and IronPython: run Python on the Java Virtual Machine and .NET respectively.

Recent CPython releases have also been adding performance work, including an experimental JIT, so check the release notes on python.org for the current status.

How to answer this in an interview

Keep it to three sentences: Python is called interpreted because there is no separate compile step to a native executable and the code is executed by an interpreter at run time. Internally, CPython compiles source to bytecode and the Python Virtual Machine interprets that bytecode. Imported modules have their bytecode cached as .pyc files. For more questions like this, see our Python interview questions for freshers, and if you are just starting, follow our 60-day plan to learn Python.

Frequently asked questions

Is Python compiled or interpreted?

Both, in a sense. CPython compiles source code to bytecode automatically, then interprets the bytecode. Because there is no separate compile step to machine code, Python is classed as an interpreted language.

What is the Python Virtual Machine?

It is the part of the Python interpreter that executes bytecode instructions one by one. It is a program loop inside CPython, not a separate virtual machine you install.

Does Python check syntax before running?

Yes. The whole file is parsed before execution starts, so a syntax error stops the program before the first line runs. Errors such as a misspelt variable name are only found when that line executes.

Is Java an interpreted language like Python?

Java is usually described as compiled, because you run javac to produce .class bytecode files before running them. The JVM then interprets and JIT-compiles that bytecode. Python does its bytecode step automatically and normally runs it without a JIT.

Read more in our Python section.

Other articles

Radium4 — IT training blog

Practical, plain-English guides for DevOps, cloud, data, Python, testing and security learners.