Skip to main content

Command Palette

Search for a command to run...

Python Inner Working

Published
•2 min read•View as Markdown
B

Driven by Curiosity, Building the Web

When you write Python code in a .py file, Python does not directly execute that file line by line.

Instead:

  1. Python reads your .py file.

  2. It compiles the source code into bytecode.

  3. That bytecode is then executed by the Python Virtual Machine (PVM).

Bytecode is:

  • A low-level language

  • Platform independent

  • Not machine code

  • Designed to be executed by the Python Virtual Machine

Because bytecode is already compiled to an intermediate form, it runs faster than raw source code.

What is .pyc File?

When Python compiles your code, it creates a compiled file with extension:

This file contains the compiled bytecode version of your program.

Example:

🔹 What is __pycache__?

When you import a Python file, Python automatically creates a folder:

Inside this folder, Python stores the compiled .pyc files.

Example:

  • cpython → implementation name

  • 313 → Python version (3.13)

  • .pyc → compiled Python bytecode

These .pyc files are sometimes referred to as complied python file or frozen binaries.

They help Python:

  • Avoid recompiling code every time

  • Improve performance on repeated imports

Python Virtual Machine (PVM)

After bytecode is generated, it is executed by the Python Virtual Machine (PVM).

The PVM is:

  • Also known as the Python Interpreter

  • A runtime engine

  • A loop that reads and executes bytecode instructions

    CPython and Other Python Implementations

    🔹 CPython (Standard Implementation)

    • Most widely used Python implementation

    • Written in C

    • Converts Python code → Bytecode → Executes via PVM

When you install Python from python.org, you are usually installing CPython.


🔹 Other Python Implementations

Besides CPython, there are other implementations:

  • Jython → Runs on the Java Virtual Machine (JVM)

  • IronPython → Runs on .NET framework

  • PyPy → Faster implementation with JIT (Just-In-Time compilation)

  • Stackless Python → Focused on concurrency and microthreads

Each implementation may handle execution slightly differently, but the concept of bytecode and virtual machine still applies.