Skip to content

Code interpretation

Python Interpreter

Python is both a programming language and software called an interpreter. Interpreter is a program thanks to which it is possible to run written scripts (files that are directly launched, constituting the input to the application) and modules (files intended to be imported by other files). Without properly installed interpreter, no coded program can be run. In an earlier block, we looked at how to run python code in an interactive session. What's going on - however - underneath that our scripts can be run?

When the python <program_name.py> command is run, a process is started to perform a series of steps that ultimately lead to the interpretation of the code.

  1. Sequential processing of code from a file, instruction by instruction - the lines are read in a similar way as a human would do: from top to bottom.
  2. Compilation of source code into so-called bytecode.
  3. Reading bytecode on a Python virtual machine.
  4. Code execution.

The Python interpreter translates our human-readable code into a series of numbers and memory addresses that must be referred to in order to properly understand and interpret this series of numbers. This generated code is called a byte code. The next step is to use this translated data string to start the program by something called the Python Virtual Machine (PVM). Thanks to this scenario, Python is independent of the environment and hardware on which programs written in that language are run. There is an additional third instance between the processor that actually executes the instructions and the code: the interpreter, which as a separate, special program is responsible for the code execution model.

interpreter

Python Virtual Machine

The Python Virtual Machine (PVM) is a Python runtime engine. It's a cycle that iterates over bytecode instructions to run them one by one.

PVM is not an isolated Python component. This is only part of the system installed on the computer. The Python virtual machine is the last element in * software * called interpreter.

Files pyc

The interpreter, reading the .py modules - files that are imported into others in the application, converts the listing contained in them to bytecode . At the same time, it creates special .pyc files with the same name as the interpreted module, but with a different extension. They are the storage location for the bytecode. It remains intact in them and when the application is restarted, the step of interpreting these files will be skipped, thanks to which everything will be done a little faster. This can be compared to searching the phone book several times: the second time, instead of starting the search again, we will move to the page marked by the tab, so we save some time. The .pyc file will change (it will be re-generated by the interpreter) if and only if the source code of the corresponding.py file changes. Files with the extension .pyc are stored in the folder __pycache__**.

vm

Interpreters

Python is just a syntax specification. When people talk about Python, they often mean not only the language but also the implementation of CPython. There are many different implementations that support this specification.

CPython

The most popular and default implementation of Python is called CPython and was written in C (hence the name). CPython provides the highest level of compatibility with Python packages and C language extension modules. There are libraries that allow you to use the code implemented in C and vice versa.

This is the implementation that we use during this course.

PyPy

pypy

PyPy is a Python implementation written in Python. PyPy strives for maximum compatibility with the reference implementation of CPython, while improving performance. JIf you want to improve the performance of your Python code, try PyPy. Many numerical operations are often faster than those performed in CPython. In the test suite it is currently over 5 times faster than CPython. What's more, all standard libraries are written in pure Python, so you don't need to know C to read them.

Jython

jython

Jython is a Python interpreter written in Java. It compiles Python code into Java bytecode (!), Which is then executed by a Java Virtual Machine (Java Virtual Machine, JVM) - something like a virtual machine in Python. By using it, we get the opportunity to use Java libraries.

Jython currently supports versions up to Python 2.7.

IronPython

ironpython

IronPython is a Python interpreter written in C#. It can use both Python and .NET libraries, and can also share Python code with other languages in .NET.

Same as Jython, it doesn't support Python 3. implementation.