INTRODUCTION

Before talking about the topic ” Python runtime environment ” let’s make a very brief introduction. Let’s see in this article how the runtime manages the execution of programs. Let’s take a closer look at how Python executes programs through its runtime. This will be useful to us as we continue the course. Let’s first ask ourselves if Python is a compiled, interpreted language or is it also based like C# and Java on Virtual Machine?

COMPILED LANGUAGES

COMPILED LANGUAGE

PYTHON RUNTIME ENVIRONMENT – INTERPRETED LANGUAGES

INTERPRETED LANGUAGES

PYHTON RUNTIME ENVIRONMENT – LANGUAGES THAT USE BYTECODE

LANGUAGES USING BYTECODE

HOW PYTHON WORKS

THE PYTHON VIRTUAL MACHINE

PYTHON VIRTUAL MACHINE-ORGANIZATION OF PYTHON PROGRAMS

A Python program consists of source files with a main script which is usually the one we launch from the command line and a set of modules. The script contains the main flow of the program while the modules are usually libraries which we will see better later.

STRUCTURE OF A PYTHON PROGRAM

DEEPENING

The Python Virtual Machine (PVM) is an essential component of your Python implementation. It is responsible for executing Python bytecode. Simply put, the PVM takes Python source code, compiles it into an intermediate format called bytecode, and then executes this bytecode. A detailed explanation of how PVM works is provided below.

1. Python Source Code

The starting point is the Python source code written by the developer. For example:

def add( a, b ):
    return a + b

result = add( 2, 3 )
print ( result )

2. Python compiler

The Python source code is passed to a Python compiler which translates it into bytecode. Bytecode is a middle-level language that is closest to the representation that the virtual machine can understand.

Python Source Code -> Python Compiler -> Bytecode

3. Bytecode

The bytecode generated by the compiler is a set of instructions that can be executed by the Python Virtual Machine. Bytecode is usually stored in files with the . pyc .

4. Python Virtual Machine (PVM)

The PVM executes the bytecode. It reads and interprets bytecode instructions one at a time. The PVM is responsible for managing the runtime environment, including memory management, exception handling, and interaction with the operating system.

Bytecode -> Python Virtual Machine -> Execution

Detailed Operation of PVM

1. Reading the Bytecode

The PVM reads the bytecode and analyzes it. Each bytecode instruction is decoded and interpreted.

2. Execution Stack

The PVM uses an execution stack to manage operations. For example, for the add statement, the values ​​are loaded onto the stack, the operation is executed, and the result is returned to the stack.[ ]

LOAD_CONST 2 -> [2] LOAD_CONST 3 -> [2, 3] BINARY_ADD -> [5]

3. Memory Management

The PVM manages memory using a garbage collection mechanism. Objects no longer referenced are automatically removed from memory to free space.

Conclusion

The Python Virtual Machine is a fundamental part of the Python language, responsible for executing compiled Python code. Understanding how PVM works helps you write more efficient code and solve performance problems.

LINK TO GITHUB CODE

GITHUB

LINKS TO PREVIOUS POSTS

PREVIOUS POST LINKS