How Python Code Works Internally (Step-by-Step)

 


Many developers say “Python is an interpreted language”, but internally Python follows a compile + interpret model.

Let’s break it down clearly using a real example and the same pipeline shown in the diagram.


🔷 High-Level Python Execution Flow

Source Code (.py) ↓ Compiler ↓ Bytecode (.pyc) ↓ Python Virtual Machine (PVM) ↓ Running Program

🧩 Step 1: Python Source Code

This is the code we write.

Example: hello.py

x = 10 y = 20 print(x + y)

This is human-readable, not machine-readable.


🧩 Step 2: Compiler (Inside Python Interpreter)

When you run:

python hello.py

Python does NOT execute the source code directly.

What the compiler does:

  • Checks syntax

  • Converts source code into bytecode

  • Bytecode is platform-independent

📌 This compilation happens automatically — you never run a compiler manually.


🧩 Step 3: Bytecode (.pyc)

After compilation:

  • Python generates bytecode

  • Stored in __pycache__ directory

Example:

__pycache__/ hello.cpython-311.pyc

Bytecode characteristics:

✔ Low-level instructions
✔ Optimized for execution
✔ Same across Windows / Mac / Linux


🧩 Step 4: Python Virtual Machine (PVM)

The Python Virtual Machine is the heart of execution.

What PVM does:

  • Reads bytecode instruction by instruction

  • Executes operations like:

    • variable assignment

    • arithmetic

    • function calls

    • loops and conditions

Example internal execution:

LOAD_CONST 10 STORE_NAME x LOAD_CONST 20 STORE_NAME y LOAD_NAME x LOAD_NAME y BINARY_ADD PRINT

📌 This is why Python is called interpreted
the bytecode is interpreted at runtime.


🧩 Step 5: Library Modules

From your image, Library Modules feed into the Virtual Machine.

Example:

import math print(math.sqrt(25))

What happens internally:

  • Python loads compiled bytecode of math module

  • Links it into the running program

  • Executes it via PVM

📌 Even standard libraries follow the same bytecode + VM execution model.


🧩 Step 6: Running Code (Final Output)

The PVM executes bytecode and interacts with:

  • CPU

  • Memory

  • OS system calls

Output:

30

🎉 Program completed!


🧠 Why Python Feels Interpreted

Even though Python compiles first, developers experience it as interpreted because:

✔ No visible compile step
✔ Executes line-by-line behavior
✔ Errors appear at runtime
✔ Dynamic typing resolved during execution


🆚 Python vs Java (Quick Context)

AspectPythonJava
CompilationAutomaticExplicit (javac)
BytecodeYesYes
Virtual MachinePVMJVM
JIT Compilation❌ No✅ Yes
ExecutionInterpretedHybrid (Interp + JIT)

🎯 Interview-Ready Explanation

Python source code is first compiled into bytecode by the Python compiler.
This bytecode is then executed by the Python Virtual Machine, which interprets each instruction at runtime.
Libraries are also loaded as bytecode and executed within the same VM.


📝 Final One-Line Summary

Python = Compiled to Bytecode + Interpreted by Virtual Machine


No comments:

Post a Comment

Java Design Patterns With What • Why • When • Full Java Programs • Client Usage

  Java Design Patterns  With What • Why • When • Full Java Programs • Client Usage Introduction Design Patterns are proven solutions to...

Featured Posts