Python Inner Working

Dark ByteDark Byte
2 min read

If you've ever wondered "how does this python code run exactly?" – you're not alone. Let’s break it all down in full vibey mode with real examples and chill analogies..


🎯 First Things First – Meet the PVM (Python Virtual Machine)

When you hit Run on your Python file, it’s not like your computer suddenly understands Python magically.
Nope. There’s an entire invisible army behind the curtain. That army is the PVM.

So, what is this PVM?

  • Think of PVM as the interpreter engine.

  • It takes in something called byte code (we’ll get to that in a sec) and executes it line by line.

  • It’s also known as the Python Interpreter.

And guess what? There’s more than one interpreter!

  • CPython – OG Python, most widely used.

  • PyPy – Fast and furious version.

  • Jython – Python for Java world.

  • IronPython – For .NET playground.

  • Stackless – Concurrency ka baap.

Byte Code is not machine code. It’s Python-specific and needs the PVM to run.


🛠️ From .py ➝ .pyc – The Behind-the-Scenes Compilation

Let’s say you write:

print("Hello Madhav!")

Here’s what happens:

  1. Python compiles this .py file to byte code (low-level platform-independent code).

  2. This byte code is stored as a .pyc file.

  3. These .pyc files live in a folder called __pycache__.

Example:

hello_chai.cpython-312.pyc

Means: This is a bytecode file for Python version 3.12.

But wait, here’s the catch:

  • .pyc is only created for imported files, not for the top-level script.

  • Byte code runs faster because it's already compiled.

🧠 So next time you see __pycache__, don’t delete it in panic — it's just Python being smart.


🔁 Bringing It All Together :

Here’s the full step-by-step process when you run Python:

  1. Write Python Code (.py file)

  2. Python compiles it to bytecode (.pyc)

  3. PVM takes over and interprets that bytecode at runtime

  4. You see your output on the screen ✨

So basically:

.py → compiles to .pyc → executed by PVM →Output


📦 Bonus: Why This Matters to You

  • Knowing about PVM and bytecode helps you understand how Python handles performance.

  • When using tools like PyInstaller, you’re basically packaging byte code.

  • Debugging becomes easier when you know Python isn’t compiling like C++ – it’s doing something more dynamic and flexible.


Stay curious and Keep coding!

– Madhav

0
Subscribe to my newsletter

Read articles from Dark Byte directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dark Byte
Dark Byte