How Python Code is Executed?

Shruthi Shri
Oct 24, 2020

A Compiler converts Human Code into Machine Code. Machine Code is something which is specific to the type of CPU of a computer. So, if we compile a C program on a Windows machine, we can’t execute it on a Mac, because Mac and Windows have different machine codes.

Java came to solve this program. Java compiler does not compile Java Code into Machine Code. Instead it compiles into a portable language called Java bytecode, which is not specific to hardware platform like Mac or Windows.

To convert, Java comes with a program called Java Virtual Machine or JVM. When we run a Java program, JVM kicks in, loads our Java bytecode and at runtime it will convert each instruction to machine code. With this model, we can run Java bytecode on any platform that have JVM. We have JVM implementations for Windows, Mac and Linux.

JVM implementations on Windows knows how to convert Java bytecode to machine code that windows machine can understand, which applies for Mac and Linux as well.

Python have also taken the same route, so it is platform independent. However, python has four variations of compilers namely,

C Python written in C (which is default).

Jython written in Java.

Iron Python written in C#.

Pypy written in Python.

The major difference between these variations is that each compiler after converting the source code to bytecode will run in their respective virtual machines.

--

--