01. The Python Interpreter

GitBook is an amazing frontend style to present and organize contents (such as book chapters and blogs) on Web. The typical to deploy GitBook at Github Pages is building HTML files locally and then push to Github repository, usually to the gh-pages branch. However, it’s quite annoying to repeat such workload and make it hard for people do version control via git for when there are generated HTML files to be staged in and out.

This theme takes style definition out of generated GitBook site and provided the template for Jekyll to rendering markdown documents to HTML, thus the whole site can be deployed to Github Pages without generating and uploading HTML bundle every time when there are changes to the original repository.

The Python Program

The Python interpreter program is run as python and includes both the interpreter itself and the Python compiler, which is implicitly invoked, as and if needed, on imported modules.

Environment Variables

Besides PATH, other environment variables affect the python program. Some environment variables have the same effects as options passed to python on the command line, as we show in the next section. Several environment variables provide settings not available via command-line options. The following list covers just the basics of a few frequently used ones; for all details, see the online docs.

Command-Line Syntax and Options

The Python interpreter command-line syntax can be summarized as follows:

[path]python {options} [-c command | -m module | file | -] {args}

Brackets ([ ]) enclose what’s optional, braces ({ }) enclose items of which zero or more may be present, and bars (|) mean a choice among alternatives. Python uses a slash (/) for file paths, as in Unix.

Running Python Programs

Running a Python script at a command line can be as simple as:

$ python hello.py
Hello World

The filename of the script can be any absolute or relative file path, and need not have any specific extension (though it is conventional to use a .py extension).

The Python interpreter works as a computer converter that converts high-level language to low-level machine language, which is essential for the computer to understand the code written by a programmer. Python codes are executed by an interpreter called CPython, which is written in C language and executes instructions in a block of code one line after another.

Step 1: Lexing The first step of analyzing a code block in Python starts with Lexing, wherein the single line of code being executed by the interpreter is converted into smaller parts. Each of these parts is called a token, and these tokens are generated by the lexer, which is a part of the Python interpreter. Essentially, this can be considered to be a step in which the interpreter breaks down the user instruction into small pieces, to process it piecewise.

Step 2: Parsing In this step, another component of the Python interpreter called the Parser performs the process of Parsing. Now essentially, Parsing is a process in which the tokens generated in the Lexing stage into a structure called Abstract Syntax Tree. This tree shows the relationship between the tokens generated from a particular line of code. This is where the Python interpreter also checks for syntax errors, and if an error is found, the interpreter stops translating code and shows an error message.

Step 3: Creation of Byte Code After successful completion of the Parsing stage, the compiler (another part of Python interpreter) converts the Abstract Syntax Tree into an intermediate language code, called bytecode . This is a compiled version of the original code which is a low-level, platform independent representation. This byte code is stored in a file with the same name as the original source file, but with a ‘.pyc’ extension instead of ‘.py’ . This is a step completely hidden from the developer which happens internally, and is performed as executing byte code is much faster than original code statements.

Step 4: Conversion to Machine-executable Code One of the most integral parts of the Python interpreter is the Python Virtual Machine, or PVM. It is this part of the compiler which truly executes a Python program. The PVM is the actual runtime engine of Python, and can be summarized as a big loop which iterates through the byte code instructions stored in the file with .pyc extension statement wise. Then it converts the statements into machine code, that is binary (0s and 1s). It also integrates and loads the inputs and libraries involved in the program, so that the required instructions can be carried out successfully. The PVM is the part of the interpreter that truly executes Python scripts.

Step 5: Returning Output After the code is converted to binary, it is executed by the interpreter. If there is an error, it displays the message and exits. Such an error is called a runtime error. If there is no runtime error during program execution, then the interpreter prints the output and exits successfully.

results matching ""

    No results matching ""