Python is an interpreted language meaning that there will be some program called interpreter which will take in python code and convert it machine code which is then executed by machine. At the fundamental level we can say that Python interpreter takes in python code and churn it into machine code(100100010002 Just zeros and ones).
How does Python program runs? When you call python interpreter on some code file by saying python3 file.py then interpreter will read the program and converts it into machine code, which then executes on machine.
Bonus Point 🥳
How to start executing a python program? Just use python3 file.py on terminal if your on mac or command line if on windows. Like pyhton3 file.py will start interpreter to interpret python code inside file.py
Broadly we can say that interpreter firstly converts python code into byte code which then is executed by Python Virtual Machine.
Generating Byte Code
Through compilation of python code interpreter converts python code to byte code(Usually byte code is platform independent). Although this is hidden process which interpreter does, but if you want to see what byte code is interpreter generating from python code?
Then you can use Python’s dis module(dis being short for disassembler). Below is a code example showing equivalent byte code generated by Python Interpreter for a snippet of Python code.
Bonus Point 🥳
Why does Python use interpreter? Python uses interpreter for saving on time taken to execute code, as through interpreter Python code is converted to byte code which executes faster.
Moreover Python saves byte code generated into subdirectory __pycache__ into source code directory. Like if source code file have name program.py then interpreter will save program.pyc (pyc means Python Complied) into __pycache__ subdirectory.
Also whenever you run any python program interpreter looks into subdirectory __pycachce__ to find out whether .pyc file exists or not. If .pyc file exists then interpreter checks whether any changes have been made to source code since .pyc file was created, if any changes have not been made then interpreter will execute already existing .pyc file otherwise it will interpret source code file.
Python Virtual Machine
Python Virtual Machine(PVM) is just a big for loop which iterates over byte code instructions one by one and execute them.
That’s how roughly Python Programs are executed by interpreter, here I’ve just provided a brief overview of Interpreter but in case your interested in knowing little details about it then please see – Workings of Python Interpreter article which I put on website couple of days back. In this article I’ve covered Python Interpreter in a much detail by divided its execution process into 3 steps – Initialisation, Compilation and Code Object Execution.
Bonus Point 🥳
How does the Python Interpreter Work? Python interpreter starts with taking in code, parses it down to byte code and then executes it.
Moreover if your learning Python Programming Language, then please consider checking out other articles about Python – Computer Science Hub Python Programming Language.