Python is formally an interpreted language, you can look at the discussion on interpreted and compiled languages here. Python programs are executed by a software known as the Python Interpreter.
We can use the interpreter interactively(as we will see in a while) especially while debugging, but in most practical cases, the commands are defined and saved in a text file known as the source file or python script. A Python source file is conventionally saved with a .py extension/suffix for example foo.py, this is just a convention you can use a file with any valid extension such as foo.txt and the interpreter will happily execute your program. However, in reality you will almost always want to use .py extension for Python scripts, there are many reasons why you should stick to this convention, some of these reasons are outlined below:
- The Operating system will recognize the file as a Python file and not just any other file and will offer it some special support.
- Text Editors/ IDEs will treat a file with a .py extension as a Python file by default. This will allow you to access all the feature offered by the editor/IDE for Python programming such as code highlighting, auto-completion, indentation, debugging and many others without extra setup.
- The Python import system will only work for Python files with a .py extension you will not be able to import a file with some other extensions.
- Users will easily associate a .py extension with a Python file even without seeing it's content.
How does the Python interpreter Work Internally?
The Interpreter performs several operations on the source code,
-
Analyzing the source code
The interpreter starts by analyzing/parsing the source code to check that it is actually a valid Python program by ensuring that it is correctly formatted. It performs lexical analysis on the source code to ensure that Python syntax is strictly adhered to, otherwise it raises syntax error before terminating the process. It also checks to ensure that indentation rules are followed , it raises Indentation error if not.
-
Generating Bytecode
The High level Python program would be useless if there was no way to turn it into instructions that a computer will understand. Bytecode are set of intermediate instructions that are executable by the Python Virtual Machine. The Python interpreter, at this phase, turns the source code into bytecode . The bytecode are saved in a file with a .pyc extension in the same directory as the .py file and are executable from any computer which have the PVM(python virtual machine) . Any error encountered while the translation is on-going is known as a runtime error.
-
Initializing the Python Virtual Machine
The Python virtual machine(PVM) is responsible for translating the bytecode into the actual machine code, and displaying the final output. The virtual machine does all the necessary architecture adjustments, making the Python program highly portable. It executes the bytecode one statement at a time, reporting the output of that particular execution.
So the above process can be summarized as:
Interpreter Usage Modes
We can execute Python commands in two modes:
- Interactive Mode
- Script Mode
In the following section we will look at each of the two modes.
Running Python Interactively
When you open the commandline and type the word python
, the interpreter will be started in what is referred to as the interactive mode.
In the interactive mode, we type one statement in a single line, it is evaluated and the results are displayed in the console, then the interpreter waits for us to enter another statement in the next line. These makes the interactive mode to be also known as the REPL(Read, Evaluate, Print, Loop) mode, as the interpreter reads the command, evaluates it, prints the results and then this steps continues indefinitely.
One of the most basic usage of the interactive mode is as a calculator. This way, we type a math expression, and in the next line, its result/answer is displayed.
Python is highly sensitive to indentations, and any incorrectly indented code will lead to the IndentationError
exception being raised.
Literally any valid Python statement can be entered in the interactive mode, for example we can import modules, define functions and do all the other stuffs that can be done in the script mode.
Run the interpreter in script mode
The interactive mode is mostly suitable for educational, debugging or simple testing purposes. In almost all practical purposes, you will need to use the script mode because it is much more convenient than the interactive mode.
In the script mode, the statements are saved in a file with a .py
extension and then the interpreter is called from the command line with this file, the syntax is shown below:
python filename.py
Consider if we have a file called main.py
, with the following contents:
#main.py
import math
print(math.sqrt(25))
print(math.sqrt(100))
We can execute the statements in the file, by calling the interpreter from the command line as follows:
python main.py
As shown above, in the script mode only the outputs are displayed in the console, the statements are not.
Interpreter Implementations
The main and the official implementation of Python is written in the C language, and it is hence known as CPython. This is the most commonly used and it is the one you will download in the official Python page at python.org.
CPython allows us to use the speed and efficiency of the C langauge while at the same time enjoy the high-level syntax of Python.
Apart from CPython, other implementations of Python exists, however, these implementations must strictly follow the Python Language Specification which is based on CPython.
Any environment that supports the execution of programs written in Python language as specified by Python Specification can be regarded as a valid Python Implementation. There are many implementations and we will only list a handful of them.
Apart from CPython, some other common implementations includes:
- Jython - Implentation of Python in the Java platform
- PyPy - Implementation of Python in Python. That is correct.
- IronPython - Implementation of Python on the .NET Framework.
- MicroPython- Python for microcontrollers.
The presence of other implementation gives Python developer choices if, for some reason, they will to use another implementation other than CPython. For example, some implementations such as PyPy are known to be faster than CPython.
You can view the comprehensive details about the implementations here.