Python🐍 Internals: How it Really Works

Ashish DabralAshish Dabral
2 min read

If you're curious about how your Python script runs and why additional files are generated, this article is perfect for you. Today, I'll clarify any doubts you have about Python's internal workings.

Whenever we execute:

python xyz.py

our scripts are converted into Byte Code, and then this Byte Code is executed in a Python Virtual Machine (VM).

Python Execution Process

This is an intermediate step where all Python code is compiled down into Byte Code. This is low-level code, not Machine Code, and it is platform-independent.

If you ever notice a .pyc file in your code editor, this is our compiled program, often referred to as frozen binaries.

What is the Python Virtual Machine?

The Python Virtual Machine (PVM) is a software environment where we execute our scripts. In this Virtual Machine, a continuous code loop runs to iterate over the byte code. Whenever we feed any file into this VM, it executes that file line by line.

This is the primary reason why Python is also called an interpreted language, as the code gets executed line by line.

Python Virtual Machine

Demystifying Python Byte Code

Let's delve into byte code. As mentioned earlier, this byte code is not machine code, which means we cannot directly instruct hardware like Intel or Apple chips. Machine code provides direct instructions to our hardware, but Byte Code does not.

Here, byte code is a Python-specific interpretation. In simple terms, we cannot run this byte code file in another language's Virtual Machine. For instance, we cannot run this byte code file in Java's VM because it is specifically generated for Python and will execute only in Python's virtual machine.

If you want to learn more: Chai-aur-code (In Hindi).

Thanks to @Hitesh Choudhary sir for this in-depth knowledge.

0
Subscribe to my newsletter

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

Written by

Ashish Dabral
Ashish Dabral

I am a student, who loves writing technology articles and learning in public.