How is a Python program executed?

This post is lesson 3 of 54 in the subject Python Programming Language

1. How is a Python program executed?

Python is frequently referred to as an interpreted language. However, the process of executing a Python program involves both compilation and interpretation. Let’s look at the diagram below to understand better the process of executing a Python program.

First, the Python source code is written in a file with the extension .py. Then, the CPython compiler reads each line of Python code. It checks the syntax of each line of Python code. If there is an error, CPython will stop compiling and report the error.

If there are no errors, CPython compiles the Python source code into bytecode and stores it in a file with the extension .pyc or .pyo. The CPU cannot execute bytecode directly. It needs to be sent to the PVM (Python Virtual Machine) to be interpreted into machine code for execution. If an error occurs, the program execution process will be halted, and an error message will be displayed. These types of errors are referred to as runtime errors.

Why is it necessary to compile the source code into bytecode and then interpret it for execution? The benefit of this process is that the source code only needs to be compiled into bytecode once, and then this bytecode can be executed across various platforms, such as Windows, Linux, and MacOS. However, the disadvantage is that the process of running a Python program is generally slower than other languages such as C/C++, Julia, etc.

2. Example of executing a Python program

For example, there is a Python source code file sumPython.py located in the folder C:\python-examples as shown below:

a = 5
b = 10
print("Sum = ", a + b)

Use the Command Prompt to compile the Python code in the sumPython.py file. First, navigate to the current folder to the C:\python-examples folder. While in C:\python-examples, use the command python -m py_compile sumPython.py to compile the Python code.

Once the compilation is finished, a folder named __pycache__ is created, which contains the bytecode file sumPython.cpython-310.pyc.

File bytecode of Python
File bytecode of Python

The contents of the bytecode files are encoded in hexadecimal.

In a bytecode file of Python
In a bytecode file of Python

Then, navigate to the current folder to the C:\python-examples\__pycache__ folder and interpret the bytecode file sumPython.cpython-310.pyc to get the program result.

Interpret a bytecode file in python
Interpret a bytecode file in Python

Note: Typically, we use a Python IDE to run a Python program and do not need to perform these cumbersome steps. Python IDEs already support compiling and interpreting a Python program with just one button.

5/5 - (1 vote)
Previous and next lesson in subject<< Installing Python and dev environment with Visual Studio CodeBasic components and syntax in Python programs >>

Leave a Reply

Your email address will not be published. Required fields are marked *