How Python Runs Your Code: Compilation Process Explained (With First Program)

In my earlier article (https://hashnode.com/post/cmdxai7na000502le26b681e2), I talked about how programming languages act as translators between humans and machines — converting binary into human-readable form and vice versa. This enables communication between humans and computers.

💡 Terminology Alert!

  • High-Level Language: Language that is understandable by humans (e.g., Python, Java)

  • Low-Level Language: Language understandable by machines — usually binary (0’s and 1’s)


🤔 But How Does This Translation Happen?

This process of converting high-level language to low-level (machine) language is called compilation.

Now let’s understand how Python handles this process step by step.


🔁 Python Compilation Stages (5-Step Process)

  1. Source Code

  2. Python Interpreter

  3. Bytecode

  4. Python Virtual Machine (PVM)

  5. Output


🔹 1. Source Code

This is the code written by a developer — it’s the .py file you create.

Just like we save images as .jpg or .png, Python source code must be saved with the .py extension.

Example:

print("Hello, Python!!")

Save this as hello.py.


🔸 2. Python Interpreter

The interpreter is what starts the process.

  • It reads the source code

  • Compiles it to an intermediate format called bytecode, which is not human-readable but still not binary.

  • Then passes it on to the next stage

This is what allows your Python code to be run and understood by your system.


🔹 3. Bytecode (.pyc)

Once compiled, Python generates a bytecode file automatically.

  • It will be saved as a .pyc file

  • Stored inside a folder called __pycache__

  • Example: __pycache__/hello.cpython-311.pyc

This file contains a translated version of your original code — not in binary, but in a form Python can process faster.

🧠 Why it's useful:
If you run the same program multiple times, Python won’t compile it again — it just reuses the .pyc file, saving time.


🔸 4. Python Virtual Machine (PVM)

The Python Virtual Machine now steps in.

  • It reads and executes the bytecode

  • Executes it line by line

  • This is where all the actual actions (like printing, calculations, etc.) happen

You can think of the PVM as Python’s runtime engine.


🔹 5. Output

After all the behind-the-scenes work is done, Python finally shows you the output on your screen.

In our case:

Hello, Python!!

🧪 Let’s See This in Action

print("Hello, Python!!")
  1. Save this code as hello.py

  2. Run it using:

     python hello.py
    
  3. Python creates a .pyc file like:
    __pycache__/hello.cpython-311.pyc

  4. The PVM executes the bytecode

  5. Output appears:

     Hello, Python!!
    

🔁 In Short

Your code (.py)

Compiled to Bytecode (.pyc)

Run by PVM

Output on Screen


🧠 Final Thoughts

Knowing what happens behind the scenes gives you more control and confidence as a Python programmer. You’re not just writing code — you understand how it runs.

In the next article, we’ll take our first step into actual Python coding with variables and data.

Thanks for reading! 😊

0
Subscribe to my newsletter

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

Written by

Tammana Prajitha
Tammana Prajitha