What happens when we type python Hello.py in cmd?

1. Parsing the Command:

  •   1st the command is split into two parts: python and Hello.py.
  •   The Python interpreter recognizes the first part (python) as an executable.
  •  The second part (Hello.py) is interpreted as the name of the Python script to be executed.

2. Finding the Script:

  • The Python interpreter searches for the script file Hello.py in the current working directory (the directory you're currently in at the command prompt).

3. Compilation :

  • When you execute a Python script (e.g., python Hello.py), the Python interpreter first compiles the source code into bytecode. Bytecode is a low-level, platform-independent representation of your Python code.
  • The bytecode generated by the Python compiler is often hidden from the user, and stored in .pyc (Python compiled) files. These files contain the bytecode representation of your Python scripts.

4. Python Virtual Machine (PVM) Execution:

  • The Python interpreter initiates the Python Virtual Machine (PVM).
  • If Hello.py is compiled to bytecode, the PVM fetches bytecode instructions from the file.
  • It decodes and executes them one by one, performing operations like calculations, calling functions, and printing output.
  • Source Code Execution (if not compiled to bytecode):
  • If Hello.py is not compiled to bytecode, the PVM directly parses and converts the source code into bytecode on the fly.
  • This bytecode is then executed similarly to the compiled version.

5. Running the Script:

  • The PVM executes the script's code sequentially, line by line.
  • If the script contains any print statements, the corresponding output will be displayed in the Python prompt.

6. Script Completion and PVM Cleanup:

  • Once the entire script finishes execution, the PVM releases any resources it was using.
  • Control returns to the Python prompt, ready for further commands.

7.__pycache__ Directory: 

  • When you run a Python script, if the interpreter detects that the corresponding .pyc file is missing or outdated, it generates a new bytecode file and stores it in a directory named __pycache__. 
  • This directory helps improve startup time and avoid unnecessary recompilations.


8. Naming Convention of .pyc Files: 

  • The .pyc files follow a naming convention that includes the name of the Python script (hello_python in your example), the Python implementation (cpython for the reference implementation), and the Python version (e.g., 311 for Python 3.11).
  •  This naming convention helps differentiate bytecode files for different scripts and Python versions.



Notes:-

when we type > python Hello.py

1. The Python software/compiler compiles to Byte code. Which is a low-level code and Platform Independent.

2. The byte code is basically hidden most of the time. it is also known as  frozen Binaries

3. when we install Python software we install the Python virtual Machine which is known as Python VM.

4. This Python VM runs the byte code. Byte Code runs faster  

5. when we run the file that has imported files, it generates a folder called __pyche__

and a file named hello_python.cpython-311.pyc

6.hello_python.cpython-311.pyc

hello->file name

cpython->python type

311->version





LET US TRY AND LEARN

Author & Editor

0 Comments:

Post a Comment