How does a Python interpreter work?


What is an Interpreter?

The Python interpreter works as a computer converter that converts high-level language to low-level machine language, which is essential for the computer to understand the code written by a programmer. Python codes are executed by an interpreter called CPython, which is written in C language and executes instructions in a block of code one line after another.

Steps used

  • Lexing

  • Parsing

  • Creation of byte code

  • Conversion to machine-executable code

  • Returning output

Let’s dive into the steps in detail.

Step 1: Lexing

The first step of analyzing a code block in Python starts with Lexing, wherein the single line of code being executed by the interpreter is converted into smaller parts. Each of these parts is called a token, and these tokens are generated by the lexer, which is a part of the Python interpreter. Essentially, this can be considered to be a step in which the interpreter breaks down the user instruction into small pieces, to process it piecewise.

Step 2: Parsing

In this step, another component of the Python interpreter called the Parser performs the process of Parsing. Now essentially, Parsing is a process in which the tokens generated in the Lexing stage into a structure called Abstract Syntax Tree. This tree shows the relationship between the tokens generated from a particular line of code. This is where the Python interpreter also checks for syntax errors, and if an error is found, the interpreter stops translating code and shows an error message.

Step 3: Creation of Byte Code

After successful completion of the Parsing stage, the compiler (another part of Python interpreter) converts the Abstract Syntax Tree into an intermediate language code, called bytecode . This is a compiled version of the original code which is a low-level, platform independent representation. This byte code is stored in a file with the same name as the original source file, but with a ‘.pyc’ extension instead of ‘.py’ . This is a step completely hidden from the developer which happens internally, and is performed as executing byte code is much faster than original code statements.

Step 4: Conversion to Machine-executable Code

One of the most integral parts of the Python interpreter is the Python Virtual Machine, or PVM. It is this part of the compiler which truly executes a Python program. The PVM is the actual runtime engine of Python, and can be summarized as a big loop which iterates through the byte code instructions stored in the file with .pyc extension statement wise. Then it converts the statements into machine code, that is binary (0s and 1s). It also integrates and loads the inputs and libraries involved in the program, so that the required instructions can be carried out successfully. The PVM is the part of the interpreter that truly executes Python scripts.

Step 5: Returning Output

After the code is converted to binary, it is executed by the interpreter. If there is an error, it displays the message and exits. Such an error is called a runtime error. If there is no runtime error during program execution, then the interpreter prints the output and exits successfully.

Conclusion

Here we have understood how the CPython interpreter works while executing a Python code script. There can be slight variations with the change of interpreters, but roughly these are the essential steps which are carried out for executing the code. The usefulness of the Python interpreter in smooth debugging is one of the major reasons why Python is one of the most popular programming languages today.

Updated on: 24-Mar-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements