Python Interpreter
We often say Python is an "interpreted language", which means the Python code we write needs to be executed through a program called an "interpreter". Understanding what an interpreter is and how it works helps us understand Python more deeply.
What is an Interpreter?
Simply put, the Python interpreter is a program that reads your Python code (.py files), translates it into instructions that computer hardware can understand, and then executes those instructions.
This process is roughly divided into two steps:
- Compile to Bytecode: The interpreter first compiles your source code (human-readable text) into an intermediate form called "bytecode" (
.pycfiles). Bytecode is a low-level, platform-independent representation that is closer to machine code than source code, making it more efficient to execute. - Execute by Virtual Machine: Then, this bytecode is executed by the Python Virtual Machine (PVM). The PVM is part of the interpreter and is the engine that actually runs the code.
Because of the compilation to bytecode step, strictly speaking, Python is a hybrid of "compiled" and "interpreted", but we still usually call it an interpreted language because it provides an interactive programming experience.
Two Working Modes of the Interpreter
1. Interactive Mode
This is a powerful feature of the Python interpreter, also known as REPL (Read-Eval-Print Loop).
You can start interactive mode by typing python or python3 directly in a terminal (command prompt or PowerShell). You'll see a prompt, usually >>>.
$ python
Python 3.9.7 (default, Sep 10 2021, 14:59:43)
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>Now, you can type any Python code directly, and after pressing Enter, the interpreter will immediately execute and print the result.
>>> print("Hello, World!")
Hello, World!
>>> 2 + 3
5
>>> name = "Python"
>>> name
'Python'Interactive mode is very suitable for:
- Quickly testing a small piece of code.
- Learning and exploring language features.
- Doing simple calculations.
- Debugging.
To exit interactive mode, you can type exit() or press Ctrl+D (on Unix-like systems) or Ctrl+Z then Enter (on Windows).
2. Script Mode
This is the most common way to run Python programs. You save your code in a file ending with .py (e.g., my_script.py), then tell the interpreter to execute that file.
my_script.py:
# This is a Python script
name = "Alice"
print(f"Hello, {name}!")In the terminal, use the python command with the filename to run it:
$ python my_script.py
Hello, Alice!The interpreter will execute all code in the file from start to finish.
Different Python Interpreter Implementations
When we say "Python", we usually refer to CPython. This is the official, C language implemented, most commonly used and standard Python interpreter.
But besides CPython, there are other interpreter implementations for different purposes:
- Jython: Implemented in Java language, can compile Python code into Java bytecode and run on the Java Virtual Machine (JVM). This allows Python to seamlessly integrate with Java code and libraries.
- IronPython: Implemented in C#, runs on the .NET platform. Similar to Jython, it allows Python to integrate with the .NET ecosystem.
- PyPy: Implemented in Python language itself (more precisely RPython). It includes a "Just-In-Time compiler" (JIT), which can significantly improve the execution speed of long-running programs, making it a high-performance alternative to CPython.