# 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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729528662407/345cb71d-d2ba-4230-afc4-2bbae45d7528.png align="center")

### 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`](http://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.
