Python Inner Workings


Ever wondered what goes on behind the scenes when you run a Python script? You write a file, run it with Python, and see the output. Simple, right? But beneath the surface, Python is doing some pretty fascinating work. Let’s unravel how Python executes your code.
The role of the interpreter
To run a Python program, you need a Python interpreter. When you execute a script (say hello.py
), the interpreter processes all the instructions inside the file — whether it's a print statement or a complex calculation.
The very first thing is, our instructions are converted to byte code, which is mostly hidden, but in some cases it is visible (when we import any function from any other file, then we were able to see that pycache folder, inside which we had a file with extension .pyc)
But what happens internally is more interesting:
Step-by-Step Breakdown of Python Execution
1. Compilation to Bytecode
Wait, isn't Python an interpreted language? Yes — but it does involve a compilation step. Python source code (.py
) is first compiled into bytecode, a low-level, platform-independent representation of your code. Think of it as a translation from human-readable code to a language the Python interpreter understands.
This is not machine code. Bytecode is not directly executed by your CPU but is intended for another layer — the Python Virtual Machine (PVM).
💡 Why Bytecode?
It runs faster because parsing and syntax checking are already done.
It's platform-independent — works the same on macOS, Windows, or Linux as long as there's a Python VM.
2. The pycache Folder and .pyc
Files
Sometimes, after importing a module in Python, you’ll notice a __pycache__
folder appears in your project. That’s Python caching the bytecode version of the imported module.
Example: Suppose you have two files:
If you import something from hello.py
inside bye.py
, Python creates a file like:
__pycache__/hello.cpython-313.pyc
What does this mean?
.pyc
= Python Compiled file.cpython-313
= Compiled using CPython version 3.13.
Benefits:
- Faster future imports — Python skips the compilation and directly loads the bytecode.
📝 Note:
This
.pyc
file is only created for imported modules, not top-level scripts.These compiled files may be regenerated as your source code changes.
Multiple versions might be maintained if you're running different Python versions.
3. Python Virtual Machine (PVM)
After bytecode is generated, it's passed to the Python Virtual Machine, also known as:
Runtime Engine
Python Interpreter
🌀 What does it do?
It’s a loop that continuously reads bytecode and executes it line by line.
It acts like a software CPU, interpreting bytecode and performing operations.
⚠️ Important Note:
Bytecode is NOT machine code. You can't give it directly to Intel or Apple chips.
It’s designed to be understood by the PVM, not by hardware.
This architecture gives Python its flexibility and cross-platform nature.
Summary: What Happens When You Run a Python Program
Your
.py
file is compiled into bytecode (.pyc
), often cached in__pycache__
.The bytecode is sent to the Python Virtual Machine (PVM).
The PVM interprets and executes the bytecode line by line.
This whole process is seamless for you — but now you know the magic behind the scenes.
Subscribe to my newsletter
Read articles from Harsh Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Harsh Gupta
Harsh Gupta
I am a CSE undergrad learning DevOps and sharing what I learn.