Python: under the hood

Photo by Chris Ried on Unsplash

Python: under the hood


Python is a very interesting language and there are many things which are being done for you under the hood to run your python file. Let us dive into the world of python and explore how things work under the hood.

Scenario overview

Suppose you are working on a project using python and you are importing some functions from another file in your directory. Then you use it and run your file. You would have noticed that a new directory is created in named "__pycache__". Note there are two underscores before and after the word pycache. So do is this new directory created and what's inside it? Let us explore.

File where you have written your function(hello.py):

def print_hello():
    print("Hello world")

File where you have imported and used your function:

import print_hello from hello

print_hello()

You will get the desired output and a new directory is created called "__pycache__". So let us explore why it is created.

How does python run in your system?

So you write a python script, save it and hit run, then the script that you have written is compiled down to byte code and remember is it not the same byte code as the java one. The byte code is generated by an engine which is generally the cpython engine, there are other engines also (jython, pypy etc.). The byte code is also called compiled python or frozen binaries. While creating the byte code the syntax of your script is checked but not all of it and some other things are performed to optimize it. So the byte code runs faster. The extension of the byte code file is .pyc and it is the file which is generated in the pycache directory. So now you know what's inside the pycache directory now. The name of the byte code file comprises of two parts the source change and the python version for example, "hello.cpython-311.pyc".

An important thing to notice is that this byte code file is kept hidden most of the time from us and only created when optimization is required such as when exporting things from one file to another.

Where will the bytecode run? It will run on the Python Virtual Machine(PVM). It is a runtime environment where your byte code is run line by line (interpreted). The PVM or the python interpreter can also run a python script directly so most of the times the intermediate byte code file is not generated.

One point to be noted is that the byte code that is generated is NOT machine code which are direct instructions to any processor. It is a python specific, low level and platform independent code which is optimized to run faster in the PVM.

Credits: Hitesh Chaudhury (Chai Aur Code)