0% found this document useful (0 votes)
18 views7 pages

Detailed Interpreter

The Python Interpreter is a software that reads and executes Python code by converting it into bytecode, which is then executed by the Python Virtual Machine (PVM). It operates in two modes: interactive mode for direct command execution and script mode for running saved Python files. While it offers flexibility and ease of debugging, it is generally slower than compiled languages due to its line-by-line execution process.

Uploaded by

Kamble Disha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Detailed Interpreter

The Python Interpreter is a software that reads and executes Python code by converting it into bytecode, which is then executed by the Python Virtual Machine (PVM). It operates in two modes: interactive mode for direct command execution and script mode for running saved Python files. While it offers flexibility and ease of debugging, it is generally slower than compiled languages due to its line-by-line execution process.

Uploaded by

Kamble Disha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Interpreter

Introduction:
The Python Interpreter is the software that reads and executes Python code. Python is an
interpreted, high-level, general-purpose programming language, which means Python code
is not compiled into machine code directly but is interpreted line-by-line at runtime by the
interpreter.

What is a Python Interpreter?


A Python Interpreter is a program that:
• Converts the high-level Python code into bytecode.
• Then executes the bytecode using the Python Virtual Machine (PVM).
The Python Interpreter is the backbone of Python programming.
It allows us to run Python programs and interact with them using the command-line
interface or script files.

Note: “Unlike a compiler, the Python interpreter does not save machine code, but may
store intermediate bytecode (.pyc files) automatically.”
• It Converts source code into bytecode, not direct machine code.
• The bytecode is an intermediate code executed by the Python Virtual Machine
(PVM).
• In many cases, this bytecode is saved as .pyc files (in __pycache__ folder).
• However, saving the bytecode is optional and automatic—you don’t manually
compile it.

Working of Python Interpreter:


1. Source Code (.py)
You write a Python program (e.g., hello.py).
2. Compilation to Bytecode/Intermediate code (.pyc)
The interpreter compiles the source code into bytecode. This is an intermediate,
lower-level form.
3. Execution by PVM (Python Virtual Machine)
The PVM reads and executes each bytecode instruction to perform the program’s
logic.

This process is repeated for each line of code.


Execution Flow Handled by Interpreter:
Your Code (.py)

[✔] Compiler (part of interpreter)



Bytecode/Intermediate code (.pyc)

[✔] PVM (part of interpreter)



Execution (final output)
All these steps are performed inside the Python interpreter automatically.
"You don’t need to manually call a compiler or run anything separately."

Features of Python Interpreter:


• Interactive Mode: You can run Python commands directly in the terminal using the
>>> prompt.
• Script Mode: Run Python files (.py) using the command python filename.py
• Portability: Python interpreter is available for Windows, Linux, and macOS.
• Dynamic Typing: No need to declare variable types explicitly.
• Error Handling: Stops execution if an error is found and shows a traceback.
Python interpreter has an interactive mode and a scripted mode:
1. Interactive Mode
Python prompt >>> appears and the Python interpreter works on the principle
of REPL (Read, Evaluate, Print, Loop). Each command entered in front of the
Python prompt is read, translated and executed. A typical interactive session is as
follows.
>>> print("Hello, world!")

2. Script Mode
Instead of entering and obtaining the result of one instruction at a time as in the
interactive environment, it is possible to save a set of instructions in a text file, just
save using .py extension

To run Python code on terminal command: python fileName.py

Note: even though Python executes the entire script in one go, but internally it is
still executed in line by line fashion.
In case of any compiler-based language such as Java, the source code is not
converted in byte code unless the entire code is error-free. In Python, on the other
hand, statements are executed until first occurrence of error is encountered.
Role of Interpreter in Development:
• Helps test code quickly (REPL – Read, Evaluate, Print, Loop).
• Useful for debugging and learning.
• Translates human-readable code into machine-executable form indirectly.

Limitations of Interpreter:
• Slower than compiled languages (e.g., C or Java) due to line-by-line execution.
• Runtime errors are detected only when the specific code is executed.

Types of Python Interpreters:


Python supports multiple implementations/interpreters:

Interpreter Description

CPython The default and most widely used interpreter written in C. (we use this)

Jython Python implemented in Java; runs on JVM.

IronPython Python for .NET framework.

PyPy Fast Python interpreter with JIT (Just-In-Time) compiler.

MicroPython Optimized for microcontrollers.

What is PVM?
• PVM (Python Virtual Machine) is the runtime engine of Python.
It is the part of the Python interpreter that executes the bytecode (Intermediate
code) generated from your Python source code.
• It is the “brain” that understands Python bytecode and performs the instructions
line by line.
Example for understanding purpose only:
Imagine you write instructions in English, which are translated to a middle language
(bytecode).
The PVM is like a smart robot that reads those translated instructions and acts
accordingly.
Role of PVM in Python Code Execution:
When you run a Python program:
1. The Python interpreter compiles your .py file into bytecode (.pyc).
2. This bytecode is sent to the Python Virtual Machine (PVM).
3. The PVM executes the bytecode line-by-line.

Conclusion:
The Python Interpreter is the key component in running Python code. It combines
compilation to bytecode and execution using the Python Virtual Machine (PVM). This
process makes Python flexible, easy to debug, and portable across systems.

When .pyc (Python Compiled) files created ?


• .pyc files are compiled bytecode files generated by Python.
• Python automatically creates .pyc files only when you import a module, not when
you run a script directly using python myfile.py.

✓ Term Module means:

o A module is just one .py file.


o It can contain functions, classes, or variables.
o You import a module to use the code inside another code.

module = single .py file

package = folder with multiple .py files (modules)

Let's see the two cases:

Case 1: Importing a Module


Suppose you have a file called math_utils.py :
# file name: math_utils.py
def add(a, b):
return a + b

# Do not try to understand the logic at this point of time.


# Just make sure you understood that some function written in one file that we are going to
use in another file.
Now, in another file you uses that math_utils.py file:

import math_utils

 At this point, Python compiles math_utils.py into bytecode, and stores it as:
__pycache__/math_utils.cpython-38.pyc
(Python version, like 38 for Python 3.8)

Case 2: Running a Script Directly


If you run a file directly like this:
python myscript.py
Python does compile it to bytecode internally, but:
• It does NOT save the .pyc file to disk.
• It holds the bytecode in memory temporarily, just for execution.
So, no .pyc file is created in this case.
Compiler vs Interpreter

Aspect Compiler Interpreter

Execution It translates the entire program into It translates and executed code line by
Process machine code at once. line.

The compiler generates an executable The interpreter directly executes the


Output
file (machine code). program without creating a separate file.

Error It detects all errors after completing It detects and reports errors line by line,
Detection the whole program. stopping at the first error.

Speed of It is generally faster because the entire It is usually slower, as it translates code
Execution program is precompiled. during execution.

Memory It requires more memory to store the It uses less memory as no executable file
Usage compiled code. is generated.

Compiled code can be executed On the contrary, the Interpreter needs to


Reusability
multiple times without recompilation. re-execute the code each time.

C, C++, Java (JVM bytecode compiled


Examples Python, Ruby, Javascript
programs)

It is suitable for performance-critical It is ideal for scripting, development and


Use case
applications. dynamic execution.

You might also like