September 19, 2017

inner workings of python imports and module system!

Lets start with an inbuilf module ex. random or math

sys.modules is a dictionary holding the refrence to the actual module code. we can verify that by using the code below:

import sys
print sys.modules

How does python knows where to look for modules when performing an import?

it maintains a search path in sys.path. to check you current search path. try the code below:

import sys

for path in sys.path:
    print path

it would output something like: