How python works? Is it really an interpreted language?

This article discuss about how python actually works, its internal architecture, and is python interpreted, compiled, or both?

Introduction

Python according to Wikipedia is an 
"interpreted, high-level, general-purpose, programming language"
The code you write in python is converted to machine code by the python interpreter without any physical compilation. What I mean by physical compilation is that you don't need to compile the python code like you are doing in Java, C, or C++, to get the output. But hold on, because of that you can't simply say python is fully an interpreted language without understanding the internal architecture of how it is being built and how it is actually working. So in this article, we are going to discuss python and its internal architecture and is python interpreted, compiled, or both?.

What are interpreted and compiled languages?

Every low-level or high-level programming language needs a translator to convert the source code you have written in your IDE to machine code, an interpreter and compiler are those language translators that convert your source code into binary code that can be understandable by a computer. The basic difference between an interpreted language and compiled language is how they are converted to the native machine language, Interpreter converts and executes each statement written in the source code line by line while the compiler converts the entire source code to the machine code at a time and then execute it. So a compiled programming language is much faster than an interpreted programming language.

Internal working of python.

When you run a simple python code to print "Hello World" you will get the output instantly, but there are certain processes happening behind the scene, no matter if the program is simple or complex, first the source code you have written gets compiled by the compiler to give something known as bytecode. Bytecode is an intermediate code that is different from actual machine code and is being run on a virtual machine rather than a physical machine. Then the bytecode is being interpreted in the virtual machine to give to the actual output. The virtual machine which is used to run python programs is known as PVM(Python Virtual Machine). 

Here are the steps for getting executable code from the source code:
  1.  The program gets compiled by the python compiler and checks for errors, if the compiler finds an error it throws an error message to the console.
  2. If there is no error, and the source code is well-formated, the compiler converts the source code to Bytecode.
  3. The bytecode is then processed inside the Python Virtual Machine(PVM) and is being interpreted to give the actual machine code.
  4. The machine code is then executed by the CPU to return the output.

Why Virtual Machine?

In some programming languages like C or C++, it can be directly compiled to give the executable code that can be run on a physical machine. But why python has such a complex architecture of converting to bytecode and then running on the virtual machine? It is simply for achieving portability. But what is portability? Python is platform-independent which means it can be run on any device no matter if it has a different CPU architecture. Different systems have different CPU architectures, so the code that you have written in your machine may not work on any other machine, so it is better to convert the source code to bytecode and executed it on a virtual machine. Then the virtual machine runs the bytecode to give that actual machine code that can be directly executed by the CPU.

Different implementations of python

Python is actually a language with syntax and semantics with some certain rules, but the implementation of python can be done differently. The default and most widely used implementation of python is CPython. CPython is the implementation of python using the C programming language. So the python code you write is compiled by CPython to get the bytecode and then it gets run on the PVM to get the machine code.

Here are some other implementations of python:

Jython - It is an implementation of python in Java language. It compiles Python source code to Java bytecode and then executes it,

IronPython - It is the .NET implementation of python by Microsoft.

PyPy - It is another alternative implementation for python language than CPython. Although it is faster than CPython.

Conclusion:

So is python interpreted, compiled, or both? We can simply say that python is an interpreted language in the first place, But to be more specific python is both an interpreted and compiled programming language. That's what I mentioned in the introduction, even if you are not physically compiling a python code it is actually happening when you run it.