Table of contents
Similar to Java, Python is an object-oriented programming language. One term for Python is an interpreted language. Functional programming languages typically used a single, lengthy list of instructions; Python, on the other hand, used interchangeable code modules. "Cpython" refers to the standard Python implementation. It is the most popular and default Python implementation.
Python's internal mechanisms
Python does not translate its code into hardware-understandable machine code. data transforms data into a format known as byte code. Compilation does occur within Python, but it does not occur in a machine language. It is into byte code (.pyc or.pyo), which the CPU is unable to comprehend. Therefore, in order to run the byte codes, we need an interpreter known as the Python virtual machine.
How Executable Code is Created from Python Source Code
To create an executable code, the Python source code passes through the following steps.
Step 1: A Python source code or instruction in the code editor is read by the Python compiler. The code begins to run at this initial step.
Step 2: Python code is written and then saved to our system as a.py file. This contains system instructions generated by a Python script.
Step 3: This is where the source code compilation stage occurs, where it is transformed into byte code. In this phase, the Python compiler additionally verifies the syntax error and creates a.pyc file.
Step 4: The Python interpreter receives the byte code in a.pyc file.Virtual Machine (PVM) is the interpreter for Python. PVM reads and runs the supplied file line by line using an interpreter that translates Python byte code into machine-executable code. A message of error is displayed and the conversion is stopped if a problem arises during this interpretation.
Step 5: The bytecode is transformed into machine code—a binary language made up of 0s and 1s—within the PVM. Because this binary language is highly optimised for the machine code, it can only be understood by the CPU of the system.
Step 6: The CPU carries out the final execution of the machine code in this step, resulting in the final outcome that your programme has predicted.
How Python Internally Works?
Code Editor: Code Editor is the first stage of programs where we write our source code. This is human-readable code written according to Python’s syntax rules. It is where the execution of the program starts first.
Source code: The code written by a programmer in the code editor is then saved as a .py file in a system. This file of Python is written in human-readable language that contains the instructions for the computer.
Compilation Stage: The compilation stage of Python is different from any other programming language. Rather than compiling a source code directly into machine code. python compiles a source code into a byte code. In the compilation stage python compiler also checks for syntax errors. after checking all the syntax errors, if no such error is found then it generates a .pyc file that contains bytecode.
Python Virtual Machine(PVM): The bytecode then goes into the main part of the conversion is the Python Virtual Machine(PVM). The PVM is the main runtime engine of Python. It is an interpreter that reads and executes the bytecode file, line by line. Here In the Python Virtual Machine translate the byte code into machine code which is the binary language consisting of 0s and 1s. The machine code is highly optimized for the machine it is running on. This binary language is only understandable by the CPU of a system.
Running Program: At last, the CPU executes the given machine code and the main outcome of the program comes as performing task and computation you scripted at the beginning of the stage in your code editor.
PYTHON MODULES AND LIBRARIES
When importing modules or libraries into a Python programme. Python first verifies if the specified module is pre-installed before running the relevant C code. The directory of the input script, directories indicated in the PYTHONPATH, and the list of directories defined in sys. path are used if the module is not built-in. Python generates a new module object if a.py file matches the imported modules. when the code in the.py file is run inside the namespace of the object. The.pyc file is the result of Python compiling source code into byte code, which enables faster execution.