Register Login

What is a Python Interpreter?

Every high level programming language is either a compiler-executed or an interpreter-based programming language. That means, the source code written by the developers is either interpreted or compiled.


Compiler executes the whole program at a time while the interpreter executes instructions line by line. In this article, we will talk about Python interpreter and its working along with some secrets that a lot of Python developers are also not familiar with.

What is a Python interpreter?

Python is an interpreted programming language written by Guido van Rossum. We call it an interpreted programming language because it executes every Python-based instructions line by line.

It can understand Python syntaxes and tokens written in a high-level language and can make it understandable to the computer. Python employs code modules, which are convertible rather than having a single long list of code that works for functional programming languages.
The classic implementation of Python is called "CPython."

There are two ways Python can interpret the code written in it. One way is through the interactive mode that is having a Python prompt & the other way is through the script mode.

Internal working of Python:

In many books and websites, it has been written that Python is an interpreted programming language. It is partially true. That is because of the internal working, which remains abstract, or you can say a mystery to a lot of Python developers and programmers.

Python does not convert its source code into machine-level instruction, which other programming languages do so that the computer hardware can understand it. Rather, it transforms the entire code into something called the byte code.

So, within Python, as an abstract form, the compilation takes place, but that compilation does not bring the entire code to machine level or assembly level as done by other compilers like C and C++.

Also, note that bytecode and assembly level instructions are not the same. The main distinction between these 2 is that bytecode gets generated within a virtual machine and for a virtual machine (intermediary system software), whereas, assembly language gets created for a CPU (so that it becomes easy and stepwise to make the hardware understand its binary form).

Every time a Python programmer executes their code, the compilation part gets accomplished first. It then generates a byte code, & internally, this byte code gets transformed by the Python Virtual Machine (PVM) that understands the underlying architecture and platform (operating system) to run the program with the desired output.

Stepwise representation of the Python Interpreter’s internal working:

  1. The Python's complete high-level instruction or source code is being read by the python compiler. Then the syntactic phase verifies whether the instructions are properly formatted, i.e., it verifies the syntactic structure of each line within the program. In case, any error encounters, it instantly stops the translation and pops up with an error message.
  2. In case there is no error, i.e. if the complete python instruction or source code is free from the syntax error, the compiler will translate the high-level instructions into its equivalent intermediate language called "Byte code."
  3. That byte code is then delivered to the Python Virtual Machine (PVM) that is actually the python interpreter. PVM helps in converts the Python's byte code into machine level instructions or binary-equivalent code. If any error occurs in this interpretation stage, then the conversion halts showing an error message.

Conclusion:

Hope this article has given you a crisp idea of what Python interpreter actually comprise of. Also, this article gave a clear understanding of the overall phases that the Python interpreter uses to perform the overall compilation and interpretation of high-level instructions using PVM.