Is Python Interpreted or Compiled?
Dive into the fundamentals of how compilers and interpreters work. Learn about the stages of compilation, the distinction between compiled and interpreted languages, and how Python uniquely combines both approaches. The document includes practical examples, making it an informative resource for understanding the core processes behind code execution.
- published
- reading time
- 2 minutes
What is a Compiler?
A compiler is a program that translates source code from a high-level programming language to a low-level programming language (e.g., assembly, object code, or machine code) to create an executable program.

- Front-end: Scans the source code, verifies syntax according to the source language, performs type-checking in statically typed languages, and reports errors if any. It then converts the code to a lower-level format for further processing.
- Middle-end: Performs optimizations, such as removing dead or unreachable code, which are not specific to the CPU architecture.
- Back-end: Carries out further optimizations specific to the target CPU architecture.
When compiling and linking code, especially for large programs, libraries may be converted to object code separately, and the linker combines them. This is why an executable file might not work when copied from one computer to another.

What is an Interpreter?
An interpreter directly executes instructions written in a programming language without first compiling them into machine language.
An interpreter uses one of the following strategies:
- Parses the source code and performs its behavior directly.
- Translates source code into some efficient intermediate representation or object code and immediately executes it.
- Explicitly executes stored precompiled bytecode made by a compiler, matched with the interpreter’s virtual machine.
There are various types of interpreters, including ByteCode, AST, and ThreadedCode.
Simply put, an interpreter processes and runs code line by line each time it’s executed.
What is a Compiled Language?
A compiled language is one in which the code is first converted into machine code by a compiler, resulting in an executable that can run on the machines it was compiled for (e.g., ARM, x64).
Examples: C++, C, Java
Suppose you have source code written in hello.cpp.
To compile this code, you’ll need a compiler, such as GCC (GNU Compiler Collection) or Clang/LLVM.
You can compile the code with the following command:
gcc hello.cpp -o hello.out
This will produce an executable file named hello.out, which you can run using:
./hello.out
What is an Interpreted Language?
An interpreted language is one in which the source code is interpreted by an interpreter and then executed line by line.
Examples: JavaScript, PHP
Which One Does Python Fall In?
Python is both interpreted and compiled. When you run it interactively in shell its interpreted while when you execute a python file, all its imports are compiled into ByteCode and then executed by the Python Virtual Machines (which have Interperter).

How Are the .pyc Files Generated?
These files are generated whenever a module is imported into a script and run.