Contents:
- What is Python Interpreter?
- Download and Install Python Interpreter
- Python Program Execution
- What is Bytecode?
- The Python Virtual Machine(PVM)
What is Python Interpreter?
Before discussing Python interpreter, let us understand what is Interpreter. Interpreter is a kind of program that executes other program.
When you write a Python program, the Python interpreter reads your program, executing it line by line and translating it into machine code that the computer can directly run.
Download and Install Python Interpreter
You need to first install the interpreter software in your machine to interpret the python program developed by you. To install the interpreter software, you need to go to official Python website, go to Downloads menu, click to download button and install it in your computer.
When Python software is installed, it generates a number of components—minimally, an interpreter and a support library. So, the Python code you write must always be run by this interpreter. And to enable that, you usually must install a Python interpreter on your computer.
Python Program Execution
In its simplest form, a Python program is just a text file containing Python statements. For example, if we type the following python statement into a file named m.py then we can call m.py a python script or program.
You can create such a file of statements with any text editor you like. By convention, Python program files are given names that end in .py; technically, this naming scheme is required only for files that are “imported”. After you’ve typed these statements into a text file, you must tell Python interpreter to execute the file—which simply means to run all the statements in the file from top to bottom, one after another. When you instruct Python interpreter to run your script, there are a few steps that Python interpreter carries out before your code actually starts crunching away.
Specifically, your python source code first compiled to something called “byte code” and then routed to something called a “Python Virtual Machine.” If all goes well, when you execute the file, you’ll see the results of the two print
statements show up somewhere on your computer.
Hello World
1024
What is Bytecode?
Internally, and almost completely hidden from you, when you execute a program Python interpreter first compiles your source code (the statements in your text file) into a format known as bytecode. Compilation is simply a translation step, and bytecode is a lower-level, platform-independent representation of your source code. This bytecode translation is performed to speed execution—bytecode can be run much more quickly than the original source code statements in your text file. Python system saves its bytecode in file that ends with a .pyc extension and in a subdirectory named __pycache__ located in the directory where your source files reside, and in files whose names identify the Python version that created them (e.g., m.cpython-312.pyc for Python 3.12).
The Python Virtual Machine(PVM)
Once your program has been compiled to bytecode (or the bytecode has been loaded from existing .pyc files), it is shipped off for execution to something generally known as the Python Virtual Machine. It’s not a separate program, and it need not be installed by itself. In fact, the PVM is just a big code loop that iterates through your bytecode instructions, one by one, to carry out their operations. That is, the PVM is the runtime engine in Python. It’s always present as part of the Python interpreter system. It is the component that truly runs your scripts, and is really just the last step of the “Python interpreter.”
🙂 Happy Learning !