Ankit Kumar

Oct 21, 2024 • 1 min read

How Python code is executed.

How Python code is executed.

When you run a Python script, several steps take place behind the scenes to execute your code. Here’s a brief overview of how it works:

Compilation to Bytecode

  • When we execute a Python script, the Python interpreter compiles the source code into bytecode.

  • Bytecode is a lower-level representation of the code, stored in a .pyc file within the __pycache__ directory.

  • During this process, all syntax errors are checked.

  • Bytecode instructions are platform-independent and remain the same across different operating systems.

  • We can manually generate the bytecode of file Python file using this command. python3 -m py_compile main.py

  • We can also execute the generated bytecode.`python3 __pycache__/main.cpython-310.pyc`

Execution by Python Virtual Machine (PVM)

  • Once the source code is compiled into bytecode, the Python Virtual Machine (PVM) interprets these bytecode instructions and performs the corresponding actions.

  • PVM is a part of Python Interpreter.

Original Post: https://blog.ankitdevelops.tech/how-python-code-is-executed

Join Ankit on Peerlist!

Join amazing folks like Ankit and thousands of other people in tech.

Create Profile

Join with Ankit’s personal invite link.

0

8

0