How Does Python Code Run: CPython And Python Difference

Introduction

 
Python is the high-level general-purpose programming language developed by Guido van Rossum in the late 1980s and released in 1991. It is a dynamically-typed language, which means we don't have to declare the type of the variable, and the type of the value is known when the code gets executed and the memory management is done automatically.
 
By reading this article you will understand the execution of Python code.
 

Python - Interpreted or compiled language?

 
You will have read somewhere in websites or books, that Python is an Interpreted language. But the truth is that it is both compiled and interpreted.
 
Let us learn about the various implementation techniques such as compilation and interpretation, and then we will see how Python code gets executed. Along the way you will
also get a clear idea about the above-mentioned truth.
 
Compilation
  • Compilation is the process in which the source code is translated into the target language, where the target language is sometimes machine-readable code and in other times it serves as the input to another compiler or interpreter. 
  • It takes the entire program at a time as the input. 
  • The code is translated once into object code and can be run many times. 
  • It will not execute the machine-readable code it produced; this is simply a translation step.
  • It is the step done before the program execution process.
How Does Python Code Run - CPython And Python Difference
Interpretation
  • Interpretation is the process in which it reads the source code and immediately reacts to them. 
  • Interpreter executes the instruction specified in the source code while the program gets executed.
  • It takes a single line of code at a time and executes it.
Interpretation uses different strategies for program execution
  1. It takes the source code, parse it and reacts based on the instruction specified.
  2. The source code is translated into some kind of intermediate representation and executes it.
  3. Executes the compiled code that is done by the compiler previously.
In the last 2 cases the compiler is part of the interpreter system.
How Does Python Code Run - CPython And Python Difference

Both Compilation and Interpretation

 
Let's see strategy 2 specified above, in detail.
 
Many programming languages don't compile to actual machine code, but to a bytecode. The Compiler first compiles the source code to an intermediate language called Bytecode, which is the lower level, platform independent representation of the source code. The Virtual Machine (Interpreter) then takes the bytecode and executes it.
 
How Does Python Code Run - CPython And Python Difference
 

How does Python code get executed?

 
Python follows strategy 2 of interpretation.
 
The Python software installed on your machine, has
  1. an interpreter
  2. a support library
The Interpreter is made up of,
 
Compiler
 
Compiler here acts as a translation step where it converts your Python source code into intermediate platform independent bytecode representation. Each statement of the Python source code gets translated into a group of bytecodes.
 
Python Virtual Machine
 
Python Virtual Machine is the part of the Python system that actually runs the python code that is in the format of bytecode. It takes the bytecode instructions one by one and executes the action.
 
How Does Python Code Run - CPython And Python Difference
 
Steps involved when you run your Python code,
  1. The compiler receives the source code.
  2. The compiler checks the syntax of each line in the source code.
  3. If the compiler encounters an error, it halts the translation process with an error message (Syntax error).
  4. Else if the instruction is well formatted, then it translates the source code to Bytecode.
  5. Bytecode is sent to the Python Virtual Machine (PVM)
  6. Bytecode along with the inputs and Library modules is given as the input to the PVM.
  7. PVM executes the Bytecode and if any error occurs, it displays an error message (Runtime error).
  8. Otherwise, if there is no error in execution, it results in the output.

Some of the Implementations of Python

 
Python has several implementations. The above described steps are done by Python's default implementation called CPython.
 
CPython
 
CPython is the standard implementation of Python written in C program. It is the implementation you download from python.org. It is the default Python interpreter.
 
It gets the source program and translates it into bytecode which is then executed by the CPython Virtual Machine.
 
How Does Python Code Run - CPython And Python Difference
 
Jython
Jython is the java implementation of Python where it compiles the Python code into Java Bytecode. The Java Bytecode is executed by the Java Virtual Machine (JVM).
 
Jython is slow when compared to CPython.
 
How Does Python Code Run - CPython And Python Difference
 
IronPython  
 
IronPython is another implementation of Python language written in C#. It uses .NET Virtual Machine (Microsoft's Common Language Runtime (CLR)). Other .NET languages can use the Python code easily as it uses .NET Framework, and Python Libraries. 
 
PyPy
 
PyPy is the implementation of Python programming language written in Python. Python's Interpreter is written in RPython( a subset of Python). PyPy uses something called JIT (Just-in-Time) Compilation, where the bytecode is compiled into native machine code and so it speeds up the Python code execution process. It is faster than CPython.
 
Some other implementations of Python are Cython, IPython, Psyco, Stackless Python, MicroPython etc,.
 

Difference between CPython and Python

 
So, by now you will definitely understand the difference between CPython and Python.
 
CPython is the implementation of Python you download from python.org. Python is the Programming language, where they are get executed by CPython.
 

Conclusion

 
Python Programming language still holds its place as the most lovable language because of its friendly structure. Most of the top companies like Google, Dropbox, NASA, Instagram, YouTube use Python. Python is one of the most wanted languages now-a-days, so it is important to learn Python.
 
In this article I have explained the Python program executions and various implementations.
 
My Python article journey will continue. Comment below if you have any queries.