You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 90d8dc6
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: fundamentals/executionmodel.md
+19-4Lines changed: 19 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,19 +20,34 @@ While Python is an interpreted language, it involves a compilation step where so
20
20
21
21
Detailed Execution Steps:
22
22
23
-
1.**Source Code**: Python code is a file with a `.py` extension.
23
+
**Detailed Execution Steps**
24
+
25
+
1.**Source Code**
26
+
Python programs begin as human-readable `.py` files containing the source code written by the developer.
24
27
25
28
+++
26
29
27
-
2.**Compilation to Bytecode**: When the program is run, the Python Compiler first translates the source code into an intermediate format called bytecode. During this phase, it also checks for syntax errors. This bytecode is a set of instructions tailored for the Python Virtual Machine. This bytecode is a platform-independent representation that is not specific to any operating system or hardware. The compiled bytecode is then stored in a .pyc file. For faster loading in future runs, the bytecode is saved as a `.pyc` file in a `__pycache__` directory. If a script is run directly, e.g. python `myscript.py`, Python compiles it to bytecode in memory only. After execution no bytecode is saved. So `.pyc` files are only created when a file is imported and when chasing is enabled (which is by default).
30
+
2.**Compilation to Bytecode**
31
+
When the program runs, the Python compiler performs lexical analysis (tokenizing) and parsing to build an Abstract Syntax Tree (AST). It then generates platform-independent bytecode — a set of intermediate instructions designed for the Python Virtual Machine. Syntax errors are detected during this phase.
32
+
Bytecode is cached in `.pyc` files inside a `__pycache__` directory for faster loading on subsequent runs (primarily for imported modules). When a script is executed directly (e.g., `python myscript.py`), the bytecode is normally compiled and kept only in memory; no `.pyc` file is written unless the module is imported and caching is enabled (the default).
33
+
28
34
29
35
+++
30
36
31
-
3.**Execution by the Python Virtual Machine (PVM)**: The bytecode is then passed to the Python Virtual Machine (PVM), which acts as an interpreter. The PVM reads the bytecode instructions and translates them into machine-specific (binary) code that the computer's CPU can understand and execute.
37
+
38
+
39
+
3.**Execution by the Python Virtual Machine (PVM)**
40
+
The bytecode is handed to the Python Virtual Machine (the interpreter). The PVM reads and executes the bytecode instructions one by one. During execution it may issue operating-system system calls and perform I/O operations.
41
+
32
42
33
43
+++
34
44
35
-
4.**Execution and Output**: The computer's CPU executes the machine code instructions, interacts with the hardware (memory, I/O devices, etc.), and produces the final results or output of the program.
45
+
46
+
4.**Output / Results**
47
+
The final results of the program are produced and made visible to the user. The intermediate stages (compilation, bytecode, and PVM execution) remain hidden — the developer and user primarily see only the original source code and the eventual output.
48
+
49
+
50
+
+++
36
51
37
52
38
53
This flow ensures that Python is platform-independent at the bytecode level, as any system with a compatible PVM can run the same bytecode.
0 commit comments