What is a module search path in Python?
Experience Level: Junior
Tags: Python
Answer
When the Python interpreter executed the import statement, it tries to search for the module in search path.
In the example below, the interpreter sees a module basic_math is to be imported, it searches for the file name basic_math.py in the search path.
It looks into the following folders:
- The folder from which the input script was run (or the current folder if the interpreted is executed interactively)
- The list of folders from PYTHONPATH environment variable (if it is defined)
- An installation-dependent list of folders configured at the time Python is installed
Example:
File main.py
import basic_math
File basic_math.py
def add(x, y):
return x+y
Related Python job interview questions
What is the print() function good for in Python?
Python JuniorHow do you obtain a value of search path in Python?
Python JuniorWhat is the import statement good for in Python?
Python Junior