The Invent with Python Blog

Writings from the author of Automate the Boring Stuff.

Mon 10 October 2022

Is Python Compiled or Interpreted?

Posted by Al Sweigart in python   

The short answer is: Python is interpreted.

There is no separate compile step after writing Python code and before running the .py file.

The Python interpreter software you download from python.org is called CPython because it's written in C.

Python can be compiled into a binary executable with a tool like Py2Exe (on Windows), py2app (on macOS), or BeeWare (all OSes including mobile).

Funny enough, there's also PyPy, a Python interpreter written in Python.

When you run a Python program, the interpreter first compiles it to bytecode and then runs the bytecode. So you could say that Python is compiled.

Bytecode is a set of basic instructions that virtual machine (VM) software runs just like machine code is a set of basic instructions that a CPU runs.

The benefit of bytecode is that interpreter VMs on different operating systems can run the same programs. (Though operating systems were originally written to run the same programs on different computer hardware.)

Python is technically not compiled nor interpreted, because Python is a language and you can write an interpreter or a compiler for any language.

Still, most languages are considered either "compiled" or "interpreted" because they are most commonly implemented with a compiler or interpreter. In this sense, C++ is compiled and Python is interpreted.

A programming language that is interpreted is often called a scripting language, but Java interprets bytecode and isn't considered a scripting language.

A high-level language used to be any language with an abstraction layer over machine code or assembly. C used to be considered a high-level language, but is now considered a low-level or bare-metal language because it is less abstract than languages like Java, Python, or JavaScript.

Transpilers compile source code in one programming language into source code in another programming language. TypeScript is a popular transpiler that compiles source code written in the TypeScript language (which adds type safety features) into JavaScript source code so that it can be run by the JavaScript interpreter in web browsers.

There's also Just-In-Time (JIT) compilation, where a bytecode interpreter analyzes which parts of the program run the most frequently and then compiles those parts to machine code for faster execution.

Java code is compiled by the Java compiler. But it compiles to bytecode, which is interpreted by the Java VM. Still, Java is considered a compiled language rather than an interpreted language or scripting language.

As software developers add more layers of abstraction, these definitions become looser and more ambiguous. It's probably not worth it to spend time arguing over them.

If you'd like to learn how compilers work, I'd recommend first learning recursion by reading my free book The Recursive Book of Recursion and then taking a compilers course such as Dr. Aiken's online compilers course.

Learn to program for free with my books for beginners:

Sign up for my "Automate the Boring Stuff with Python" online course with this discount link.