What will happen when you run the python program ?

  • In this article lets try to understand how Python executes programs

Python Interpreter – The Begining

  • Before getting into Python , lets understand what is Interpreter ?
    • Interpreter is a software program/logic that transforms the code written by you into the format your machine can understand
  • When you install python, it generates number of components and Python interpreter is part of it. (Python has lot of flavors, In this article lets restrict to CPython.)
  • When you try to execute the python program, the following sequence of actions will be performed
    1. Your source code will be compiled into a format known as byte code
    2. The byte code is shipped for execution to Python Virtual Machine(PVM) Preview

Byte Code Compilation

  • Byte code compiled files are generally stored as pyc files
  • From Python 3.2 onwards, the byte codes are stored in a sub directory named as __pycache__
  • This is performed to speed execute as Byte code can be run more quickly than original source code
  • If the same program is executed for the second time, now the same byte code is reused by loading .pyc files
  • Python will regenerate byte code
    • When source code changes
    • When version of python used is changed.

Python Virtual Machine

  • It is runtime engine of python.
  • Its a big code loop that iterates through the byte code instructions and carries out the operations accordingly

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner