Skip to main content

Command Palette

Search for a command to run...

How Python code really Runs : Understanding Bytecode and The Python Virtual Machine

Published
3 min read
S

I’m Sandip Subedi, a science student focused on building real, long-term skills in Python, Machine Learning, and modern web technologies. I believe in learning fundamentals deeply rather than rushing through shortcuts or tutorials. My approach is simple: understand how systems work, practice consistently, and document the journey through writing. I’m steadily working toward becoming an industry-ready engineer who solves real problems with clarity and intent.

Introduction

Today, I officially started my Python Learning Journey.

Like most beginners, I initially thought Python directly converts code into machine language and runs it. But as I went deeper, I discovered something far more interesting. Python has its own internal execution process, involving Bytecode and something called Python Virtual Machine (PVM) .

Most beginner tutorials focus only on syntax, but very few explains how python actually works under the hood. And I would like to thank Chai aur Code for making this understanding much easier. So in this blog I will share what I learned today about how python executes code internally, explained in simple and beginneer-firendly terms.

What Happens When We Run a Python Script ?

When we write a Python Program, we usually save it as a .py file and run it using the python interpreter.

But Python doesn’t directly convert the code into machine level instructions.

Instead, Python follows this internal flow:

Python Source Code → Bytecode → Python Virtual Machine → Output

This entire process mostly happens behind the scenes, which is why many developers never notice it.

What is Python Bytecode ?

Bytecode is low-level representation of Python code.

Some points about Python Bytecode:

» It isn’t machine language

» It is Python Specific

» It runs faster than raw Python source code

» Generates automatically by Python

Python first compiles .py code into bytecode before executing it. This is one of the reasons Python is often called an Interpreted language with a compilation step.

.pyc Files and the __pycache__ Folder

When Python generates bytecode, it may store it as a .pyc file. This .pyc files are stored inside a folder called:

pycache

Important things to understand:
» .pyc are compiled bytecode files
» They are created only for imported modules, not for the main script
» They depend on

  1. Python version

  2. Source code

A common misconception:
.pyc files are NOT standalone executables Python is still required to run them.

Python Virtual Machine (PVM)

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

The PVM is:

  • The runtime engine of Python

  • Often referred to as the Python Interpreter

  • A loop that reads and executes bytecode instructions one by one

Popular Python implementations include:

  • CPython (most widely used)

  • Jython

  • IronPython

  • Stackless Python

Among these, CPython is the most common and is what most developers use.

Complete Python Execution Flow (Step-by-Step)

Here’s the full internal workflow of Python execution:

  1. Developer writes Python source code .py

  2. Python compiles the code into bytecode

  3. Bytecode may be stored in __pycache__

  4. Python Virtual Machine reads the bytecode

  5. Instructions are executed

  6. Output is produced

Final Thoughts

This was my Day 1 learning in Python, and understanding how python executes code internally gave me a clearer picture of the language beyond syntax.

Instead of just memorizing code, learning how things work under the hood builds long-term confidence as a developer. I will continue documenting my Python and Machine Learning journey and sharing what I learn along the way.

If you’re also starting Python, I highly recommend understanding these fundamentals early.

Thanks for reading this is just the beginning.