How is Python code compiled and executed

 

Today, we will understand how a python code is compiled and executed? What are the steps taken by the interpreter to execute a python script? So, let’s get started.

how a python program is executed, python program execution process, How is Python code compiled and executed, How code is executed in Python, python,


Whenever python is installed in your machine it basically has an interpreter and support libraries. Python uses interpreter for execution as c++ use compiler for its execution.

There are two types of the interpreter:

  1. CPython that is written using C language and is a default one.
  2. JPython that is written using java language.

Interestingly, it can be a writer and implemented in any language.

An interpreter is just like a black box that takes the source code and executes it. Let’s dig in a bit deeper to understand the working of that black box.

Most of the interpreters convert source code into a machine language that CPU can understand but Python doesn’t convert its code into machine code. It actually converts it into byte code. Byte code is a platform-independent, efficient and intermediate representation of your source code! This byte code can’t be understood by CPU. So, we need a virtual machine to execute the byte code.

In short, we can say that interpreter contains a compiler and a virtual machine for its working. The compilation is the process through which the whole program is converted into a byte code and virtual machine executes the instructions step by step.

Let’s have a look at steps taken by an interpreter to execute a python script.

Step 1: The interpreter reads a python code or instruction. Then it verifies that the instruction is well-formatted this process is called compilation, i.e. it checks the syntax of each line. If it encounters an error, it immediately halts the translation and shows an error message.

Step 2: If there is no error, i.e. if the python instruction or code is well-formatted then the interpreter translates it into its equivalent form in an intermediate language called “Byte code”. Thus, after the successful execution of Python script or code, it is completely translated into Byte code.

Step 3: Byte code is sent to the Python Virtual Machine (PVM). The byte code is executed on PVM with some additional libraries. If an error occurs during this execution then the execution is halted with an error message.

Whenever a Python script is executed, the byte code is generated in memory and is simply discarded when a program exits.

But, if a Python module is imported, a .pyc file for the module is generated which contains its Byte code.

So, whenever the module is imported next time, the byte code from the .pyc the file is used, hence skipping the compilation step!

That's all for today. You can learn more about it in the video below.

If you found this article helpful share it with others. Let me know your queries and suggestions in the comment section below. Do subscribe my site and channel as I’m going to upload a series of articles and videos on programming in python and I don’t want you to miss any update. 

Moreover, you can also contact me and share your queries on Instagram, Facebook or Twitter. I would love to provide a solution to your issues.

Stay tuned and strive to learn. Keep learning, keep growing!


Post a Comment

0 Comments