Python Inner Working


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!
Popular ones include:
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:
Python compiles this
.py
file to byte code (low-level platform-independent code).This byte code is stored as a
.pyc
file.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:
Write Python Code (
.py
file)Python compiles it to bytecode (
.pyc
)PVM takes over and interprets that bytecode at runtime
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
Subscribe to my newsletter
Read articles from Dark Byte directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
