How Python Runs Programs

Understanding how Python executes your code is fundamental to becoming an effective Python programmer. This knowledge helps you debug issues, optimize performance, and better understand what happens behind the scenes when you run a Python script.

Is Python a Programming Language or an Interpreter?

Short answer: BOTH.

Typically, we refer to Python as a programming language. However, Python is also a software package called an interpreter - a kind of program that executes other programs.

When you write a Python program, the Python interpreter reads your program and carries out the instructions it contains. In effect, the interpreter is a layer of software logic between your code and the computer hardware on your machine.

How Does an Interpreter Work?

When you instruct Python to run your script (e.g., script.py), there are several steps the interpreter carries out before your code starts executing:

Step 1: Source Code Compilation

  • First, the code is compiled into something called byte code
  • This step happens behind the scenes (there is nothing to do for the programmer!)
  • A file called script.pyc is automatically generated, containing the translation of your code into lower-level code instructions

Step 2: Python Virtual Machine (PVM) Execution

  • Then, the compiled code is routed to something called a Python Virtual Machine (PVM)
  • This step is also hidden from the programmer
  • The PVM iterates through your byte code instructions, one by one, to carry out their operations

The Three-Step Process

Here’s a simplified view of what happens when you run a Python program:

1. Source Code (script.py)
   ↓
2. Byte Code (script.pyc)
   ↓
3. Python Virtual Machine (PVM)

Key Points to Remember

Important Notes

  1. Compilation is automatic - You don’t need to manually compile Python code like in languages such as C or Java
  2. Byte code files - The .pyc files are automatically created and managed by Python
  3. Cross-platform compatibility - The same Python source code can run on different operating systems
  4. Performance implications - Understanding this process helps explain why Python might be slower than compiled languages for certain tasks

Practical Implications

Understanding Python’s execution model helps explain several important concepts:

Why Python is Called “Interpreted”

While Python does compile to byte code, this compilation happens automatically and transparently. From the programmer’s perspective, you simply run your source code directly, which is why Python is often called an “interpreted” language.

The Role of the Python Virtual Machine

The PVM is what makes Python code portable across different platforms. The same byte code can run on Windows, macOS, Linux, and other operating systems, as long as Python is installed.

Performance Characteristics

  • The compilation to byte code is fast
  • The PVM execution is where most of the time is spent
  • This is why Python can be slower than compiled languages for CPU-intensive tasks
  • However, this trade-off provides flexibility and ease of development

Common Questions

What happens to the .pyc files?

  • Python automatically creates .pyc files in a __pycache__ directory
  • These files are reused if the source hasn’t changed, speeding up subsequent runs
  • You can safely delete these files - Python will recreate them as needed

Can I see the byte code?

Yes! You can examine Python byte code using the dis module:

import dis

def simple_function():
    x = 1
    y = 2
    return x + y

# View the byte code
dis.dis(simple_function)

This will show you the low-level instructions that Python generates from your source code.

Summary

Python’s execution model involves:

  1. Automatic compilation of source code to byte code
  2. Execution of byte code by the Python Virtual Machine
  3. Transparent process that happens behind the scenes

This design makes Python easy to use while maintaining cross-platform compatibility. Understanding this process helps you become a more knowledgeable Python programmer and better understand how your code actually runs.

Next Steps

Now that you understand how Python runs programs, let’s explore How We Run Python Programs to learn about the different ways you can execute Python code in practice.